SQL语句使用小技巧

Inner join

#(inner) join 选择两个表里面的交集,下面查找所有student表里面所有学生的成绩信息,
#没有包含没有参加考试的李四,也没有列出student表不存在的007号学生 
SELECT  a.name,a.grade,b.subject,b.score FROM student a INNER JOIN scores b on a.uid= b.sid; 
inner join.png

left join

 #LEFT JOIN 已左表为基准,如果条件不成立,被join表的字段为空,放开where条件后结果同inner join,没有参加考试的学生成绩也出来了
SELECT  a.name,a.grade,b.subject,b.score FROM student a 
LEFT JOIN scores b on a.uid = b.sid  -- where not ISNULL(b.`subject`); 
left join.png

join也可以优化子查询。比如如果查询每个学生参加考试的姓名,班级和科目信息,在这个案例中如果一个学生只参加一个科目就可以用下面语句查询,但是每次从a查询一条数据都要从b查询对应科目效率地下

SELECT a.name,a.grade,(SELECT b.`subject` FROM scores b where a.uid = b.sid limit 1) FROM student a;

用join可以优化查询,而且join比上面的子查询适用范围还广,不一定要一对一。

SELECT a.name,a.grade ,b.subject FROM student a JOIN scores b ON a.uid = b.sid 
image.png

right join

#LEFT JOIN 已被join表为基准,如果条件不成立,左表的字段为空 ,下面一个未知考生的成绩也出来了
SELECT  a.name,a.grade,b.subject,b.score FROM student a RIGHT JOIN scores b on a.uid = b.sid  -- where not ISNULL(b.`subject`); 
right join.png

full join

求并集

#mysql不支持full join,可以用union all left join和right join的结果
SELECT a.name,a.grade,b.subject,b.score FROM student a 
RIGHT JOIN scores b on a.uid = b.sid 
union
SELECT a.name,a.grade,b.subject,b.score FROM student a 
LEFT JOIN scores b on a.uid = b.sid

优化聚合子查询

SELECT a.name,a.grade,b.subject,b.score FROM student a 
LEFT JOIN scores b ON a.uid = b.sid 
where b.score = (SELECT MAX(c.score) FROM scores c WHERE c.sid = b.sid)

上面的查询条件where里面有子查询,可以通过join方式优化上面的聚合查询

SELECT a.name,a.grade,b.`subject`,b.score FROM student a 
JOIN scores b ON a.uid = b.sid
JOIN scores c ON b.sid = c.sid
GROUP BY a.name,b.`subject`,b.score
HAVING b.score = MAX(c.score)
最高成绩.png

分组选择

查询每个人考试成绩最高的前两科成绩

下面这个在子查询中适用limit提示不支持

 SELECT a.name,a.grade,b.subject,b.score FROM student a LEFT JOIN scores b ON a.uid = b.sid 
 where b.score in (SELECT c.score FROM scores c WHERE c.sid = b.sid limit 2)

可以依次执行下面的语句,对每个人执行依次,但是意味着有多少人就要执行多少次,效率很低。

SELECT a.name,a.grade,b.subject,b.score FROM student a LEFT JOIN scores b ON a.uid = b.sid 
 where a.`name` = '张三' ORDER BY b.score limit 2

可以用下面一条语句查询出来

SELECT d.name,c.subject,score 
FROM (SELECT sid,subject,score,
(SELECT COUNT(*) FROM scores b WHERE b.sid = a.sid and a.score <= b.score) as cnt
FROM scores a 
GROUP BY sid,`subject`,score) c JOIN student d on c.sid = d.uid 
where cnt <= 2

行转列

可以使用cross join

SELECT* FROM 
(SELECT SUM(score) as '张三' FROM student a JOIN scores b ON a.uid = b.sid and a.`name` = '张三') a  CROSS JOIN
(SELECT SUM(score) as '王五' FROM student a JOIN scores b ON a.uid = b.sid and a.`name` = '王五') b CROSS JOIN
(SELECT SUM(score) as '赵六' FROM student a JOIN scores b ON a.uid = b.sid and a.`name` = '赵六' ) c

用case 语句会更好实现

SELECT 
SUM(CASE WHEN `name` = '张三' then score end) as 张三,
SUM(CASE WHEN `name` = '王五' then score end) as 王五,
SUM(CASE WHEN `name` = '赵六' then score end) as 赵六
FROM student JOIN scores ON student.uid = scores.sid;
image.png

列转行

下面的代码适用于把一个人有多个手机号,用逗号隔开。拆分成一个姓名对应一个手机号的情况。


序列化表.png

唯一序列号SQL

image.png

删除重复数据

假设某个学生参加考试成绩有重复的,去掉重复的,保留最好的成绩

image.png
SELECT a.* FROM scores a JOIN 
(SELECT sid,`subject`,COUNT(*) cnt,MAX(score) max_score FROM scores GROUP BY sid,`subject`) b
ON a.`subject` = b.`subject` and a.sid = b.sid and b.cnt > 1 and a.score < b.max_score

用条件子查询也可以实现

SELECT*  FROM scores WHERE (sid,`subject`) in 
(select sid,`subject`  FROM scores GROUP BY sid,`subject` HAVING COUNT(*) > 1) 
and score not in (SELECT MAX(score) FROM scores GROUP BY sid,`subject` HAVING COUNT(*) > 1)
image.png

上面选择是正确的但是删除会报错:
you can't specify target table for update in from clause
所以可以将查询需要删除的id放在一个中间表就可以了。

DELETE FROM scores WHERE uid in (SELECT uid FROM (
SELECT a.uid FROM scores a JOIN 
(SELECT sid,`subject`,COUNT(*) cnt,MAX(score) max_score FROM scores GROUP BY sid,`subject`) b
ON a.`subject` = b.`subject` and a.sid = b.sid and b.cnt > 1 and a.score < b.max_score
)c)

多属性查询

查询英语和数学成绩同时在80以上的人的信息

SELECT a.name,b.subject,b.score FROM student a left JOIN scores b on a.uid = b.sid 
WHERE b.subject in ('英语','数学') and b.score > 80

这是错误的,因为有些人根本没有参加英语考试,in的条件是只要在集合中有一个符合就可以,所以上述查出的是英语或数学在80以上的人的信息

可以用 join 求交集来实现。

SELECT a.name,b.subject,b.score,c.subject,c.score FROM student a
JOIN scores b on a.uid = b.sid and b.subject = '数学' and b.score > 80
JOIN scores c ON b.sid = c.sid  and c.subject = '英语' and c.score > 80

求参加语数英里面至少两门以上的考生的信息

SELECT a.name ,b.subject as '数学',c.subject as '语文',d.subject as '英语' FROM student a 
LEFT JOIN scores b on a.uid = b.sid and b.subject = '数学'
LEFT JOIN scores c on a.uid = c.sid and c.subject = '语文'
LEFT JOIN scores d on a.uid = d.sid and d.subject = '英语'
image.png
WHERE
  (CASE WHEN b.subject is null  THEN 0 ELSE 1 END) 
+ (CASE WHEN c.subject is null  THEN 0 ELSE 1 END) 
+ (CASE WHEN d.subject is null  THEN 0 ELSE 1 END) >= 2
image.png

上面实现方式在条件增多的情况下语句过长可以使用

SELECT a.name FROM student a JOIN scores b ON a.uid = b.sid WHERE b.subject in ('英语','数学','语文')
GROUP BY a.name HAVING COUNT(*) >=2

但是这样只是过滤出姓名,具体是哪一科目还需要自己查。

你可能感兴趣的:(SQL语句使用小技巧)