1)单表查询

查询语句的格式

1
2
3
4
5
6
7
select 字段1,字段2,字段3,....,字段n         6
from 表名 1
where 条件子句 2
group by 分组子句 3
having 条件子句 4
order by 排序子句 5
limit 分页子句

例如:select *from person
该语句中,*代表了表中所有的字段,但是我们日常开发中,不允许这样使用。一定要明确的标出需要查询的相关字段名。这有有利于在查询时减少数据库在查询表时不必要的字段检索。

关系运算与逻辑运算
逻辑与 and

1
select pno, pname, age, hire_date, salary from persons where hire_date>='2018-03-01' and hire_date<='2018-04-30';

等价于

1
select pno, pname, age, hire_date, salary from persons where hire_date between '2018-03-01' and '2018-04-30';

第二种写法的效率会更优。因为采用了关系表达式,使用一次比较实现条件。

逻辑或 or

1
select pno, pname, age, hire_date, salary from persons where salary=3459 or salary = 3400;

等价于

1
select pno, pname, age, hire_date, salary from persons where salary in(3459,3400);

逻辑非 not

1
select pno, pname, age, hire_date, salary from persons where hire_date NOT between '2018-03-01' and '2018-04-30';

排序子句:order by(排序子句一定要放在所有子句之后)
sql中排序分为升序与降序,默认情况下为升序,如果要使用降序采用desc关键字。

1
select pno, pname, age, hire_date, salary from persons order by salary desc;

注意,如果出现多个排序条件时,表示为先按照第一个排序条件进行排序,如果出现相同的排序字段内容时,再按照第二个排序条件进行排序,依次类推。多个排序条件之间,用,号隔开。

1
select pno, pname, age, hire_date, salary from persons order by salary desc, hire_date;

分组子句:group by
如果SQL中使用了分组子句,那么在select子句里面,只能使用分组字段或者是统计函数。例如:

1
select age,count(age) from persons group by age;

注意:通常情况下分组子句会结合统计函数一起使用。

having条件子句
having子句通常是配合group子句一起使用,having将统计函数作为其条件字段。

1
select age,count(age) from persons group by age having count(age)>1;

limit子句:分页子句
limit 开始的行号(第一行为0),显示几条记录

1
select * from persons limit 0,1;

distinct去重:
去除指定字段中的重复内容,只保留一个。

1
select distinct salary from persons;

列表名与表别名
列别名:给指定的列,起别名。可以使用as关键字。

1
select 列名 [as] 别名 from 表名

例如:

1
select pno as '编号' from persons;

表别名:给指定的表,起别名。使用as关键字修饰。

统计函数:

1
2
3
4
5
count():统计个数
max():求最大值
min():求最小值
avg():求平均值
sum():求和

注意,在使用统计函数时,如果没有配合group by子句使用,那么在select中只能输出统计函数的值,不能输出其他字段。例如:

1
select max(salary) from persons;

其中,max、min、avg、sum只能统计数据类型是数值型的字段,如果该字段值为null,那么统计函数将忽略该内容。我们可以通过ifnull(字段,替换值),将null值转换为我们指定的值,进而参加相关的统计运算。

1
select avg(ifnull(salary,0)) from persons;

模糊查询:like
我们通常在查询时,如果条件内容不是非常明确的情况下,我们会使用like关键字进行模糊查询。通常会使用%、*来配合,其中%代表了0到n个字符,*代表了1个字符。

1
2
select * from persons where pname like '_i%';查找名字第二位是i的人员信息
select * from persons where pname like '%i%';查找名字中有i的人员信息

多表查询

1、查询分类
内连接(等值连接)

1
inner join
1
select e.empno,e.ename,e.sal,e.deptno,d.dname from emp e inner join dept d on e.deptno=d.deptno where e.sal>2000;

等价于

1
select e.empno,e.ename,e.sal,e.deptno,d.dname from emp e, dept d where e.deptno=d.deptno and e.sal>2000;

外连接
分为左外连接与右外连接

1
2
left join on
right join on

何时使用外连接?
当进行两个表连接查询时,其中一方有不匹配记录,如果进行等值连接这些记录将不会被查询检索,如果我们需要查询时将匹配记录以及不匹配记录都查询出来,就需要使用外连接。那么不匹配记录存在的表位于连接语句的左侧,就使用左外连接,如果出现在右侧就使用右外连接。
例如:其中dept表中有不匹配的记录,deptno=40的记录。
左外连接

1
select e.empno,e.ename,d.deptno,d.dname from  dept d left join emp e on e.deptno=d.deptno;

右外连接

1
select e.empno,e.ename,d.deptno,d.dname from emp e right join dept d on e.deptno=d.deptno;

子查询
概念:将一个SQL查询语句的返回结果,作为另外一个查询语句的条件来使用。
分为单行子查询与多行子查询

1
2
单行子查询:作为条件返回的查询语句,其返回结果为一行记录。
多行子查询:作为条件返回的查询语句,其返回结果为多行记录。

补充:

行变列

1
select name,max(case course when 'math' then score else 0 end) '数学',max(case course when 'english' then score else 0 end) '英语',max(case course when 'chinese' then score else 0 end ) '语文' from grade group by name;
1
2
3
4
5
6
 Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| course | varchar(10) | YES | | NULL | |
| score | int(3) | YES | | NULL |

列变行

1
select name,'math' course,math_score as score from grade2 union select name,'english' course,en_score as score from grade2 union select name,'chinese' course,cn_score as score from grade2 order by name,course;
1
2
3
4
5
6
7
8
9
------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
| math_score | int(3) | YES | | NULL | |
| en_score | int(3) | YES | | NULL | |
| cn_score | int(3) | YES | | NULL | |
+------------+-------------+------+-----+---------+----------------+

站内搜索

没有找到内容!