高级查询
使用LIKE、BETWEEN、IN进行模糊查询
模糊查询—LIKE
查询时,字段中的内容并不一定与查询内容完全匹配,只要字段中含有这些内容
select id,name from users where name like '%b%'
模糊查询—IS NULL
把某一字段中内容为空的记录查询出来
模糊查询—BETWEEN
把某一字段中内容在特定范围内的记录查询出来
--between...and
select id,name from users where id between 1 and 2
select id,name from users where id >=1 and id<=2
模糊查询—IN
把某一字段中内容与所列出的查询内容列表匹配的记录查询出来
--in
select id,name from users where id in(1,2,3)
在查询中使用聚合函数(sum,avg,max,min,count)
--sum sum
select sum(id) as 总和 from users
select avg(id) as 平均 from users
--max min
select max(id) as 最大,min(id) as 最小 from users
--count
select count(*) as 总人数 from users
select count(password) as 总人数 from users
<1>内联结(INNER JOIN)
<2>外联结
<3>左外联结 (LEFT JOIN)
<4>右外联结 (RIGHT JOIN)
<5>完整外联结(FULL JOIN)
<6>交叉联结(CROSS JOIN)
--内连接查询(跟表的位置无关)
select u.name,s.grade from score as s inner join users as u on s.uid=u.id
select u.name,s.grade from users as u inner join score as s on s.uid=u.id
-左外连接查询(跟表的位置有关)
select u.name,s.grade from score as s left join users as u on s.uid=u.id
select u.name,s.grade from users as u left join score as s on s.uid=u.id
--右外连接查询(跟表的位置有关)
select u.name,s.grade from score as s right join users as u on s.uid=u.id
select u.name,s.grade from users as u right join score as s on s.uid=u.id
分组查询GROUP BY
select id,avg(id) as 平均 from users group by id
select id,avg(id) as 平均 from users group by id,name
进行多表联结查询
分组查询—对比
WHERE子句从数据源中去掉不符合其搜索条件的数据
GROUP BY子句搜集数据行到各个组中,统计函数为各个组计算统计值
HAVING子句去掉不符合其组搜索条件的各组数据行
GROUP BY子句搜集数据行到各个组中,统计函数为各个组计算统计值
HAVING子句去掉不符合其组搜索条件的各组数据行