作业
image.pngimage.png

  1. -- 所有信息
  2. select * from stu_54;
  3. select * from exam;
  4. SELECT stu_54.code,name,subject,score from stu_54,exam
  5. WHERE stu_54.code = exam.code;
  6. select stu_54.code, name, subject,score from stu_54
  7. INNER JOIN exam
  8. On stu_54.code = exam.code;
  9. -- 张三的数学
  10. SELECT stu_54.code,name,subject,score from stu_54,exam
  11. WHERE stu_54.code = exam.code
  12. AND name = "张三"
  13. AND subject = "数学";
  14. --
  15. select stu_54.code, name, subject,score from stu_54
  16. INNER JOIN exam
  17. On stu_54.code = exam.code
  18. AND name = "张三"
  19. AND subject = "数学";
  20. -- 查看英语成绩
  21. SELECT stu_54.code,name,subject,score from stu_54,exam
  22. WHERE stu_54.code = exam.code
  23. AND subject = "英语";
  24. select stu_54.code, name, subject,score from stu_54
  25. INNER JOIN exam
  26. On stu_54.code = exam.code
  27. AND subject = "英语";
  28. -- 各科的平均成绩
  29. SELECT subject, avg(score) from exam
  30. GROUP BY subject;
  31. -- 总成绩
  32. SELECT stu_54.code,name, sum(score) from stu_54,exam
  33. WHERE stu_54.code = exam.code
  34. GROUP BY stu_54.code
  35. ORDER BY sum(score) DESC;
  36. --
  37. select stu_54.code,name,sum(score) from stu_54
  38. INNER JOIN exam
  39. ON stu_54.code = exam.code
  40. GROUP BY stu_54.code
  41. ORDER BY sum(score) desc;

外连接

  • left join: 左连接, 返回包括左表中的所有记录和右表中连接字段的相等的记录,如果右边的表中没有对应的数据,结果显示为 Null;
  • right join: 右连接, 返回包括右表中的所有记录和左表中连接字段的相等的记录,如果左边的表中没有对应的数据,结果显示为 Null;

在公司中 95%的程序员在进行外连接的时候,喜欢使用左连接进行查询。

现在有两个表
学生表和签到表
image.pngimage.png

-- 查询classmates表中 所有学员的签到情况,学号,学员姓名,签到时间,如果没有签到,则签到时间显示为空
select classmates.nu,name,checktime from classmates
left join checkout on classmates.nu = checkout.nu;

image.png
image.png

-- 查询 每个同学的签到次数, 如果没有签到,显示0
select classmates.nu,name,count(checktime) 签到次数 from classmates 
LEFT join checkout on classmates.nu = checkout.nu
group by classmates.nu,name
order by classmates.nu; -- 根据学号进行排序

image.png

-- 查询 每个同学的签到次数, 如果没有签到,显示0
select classmates.nu,name,count(checktime) 签到次数 from classmates 
LEFT join checkout on classmates.nu = checkout.nu
group by classmates.nu,name
order by classmates.nu; -- 根据学号进行排序

image.png

-- 统计出考勤最多的次数是多少次?
select count(checktime) 签到次数 from classmates 
LEFT join checkout on classmates.nu = checkout.nu
group by classmates.nu,name
order by count(checktime) desc  -- 根据签到次数进行排序
limit 1;

image.png

-- 统计考勤次数排名第二的次数是多少次?
select distinct(count(checktime)) 签到次数 from classmates 
LEFT join checkout on classmates.nu = checkout.nu
group by classmates.nu,name
order by count(checktime) desc 
LIMIT 1,1;

image.png

-- 签到次数第二高 人员

select classmates.nu,name,count(checktime) 签到次数 from classmates 
LEFT join checkout on classmates.nu = checkout.nu
group by classmates.nu,name
HAVING count(checktime)=(
    select distinct(count(checktime)) 签到次数 from classmates 
    LEFT join checkout on classmates.nu = checkout.nu
    group by classmates.nu,name
    order by count(checktime) desc
    LIMIT 1,1
);

image.png

全连接


(select * from classmates
LEFT JOIN checkout
on classmates.nu = checkout.nu)
UNION
(select * from classmates
RIGHT JOIN checkout
on classmates.nu = checkout.nu)

两个表连在一起
image.png

image.png

三表联查


-- 查询所有订单的信息, 订单id,订单所属顾客id,顾客姓名,商品名称,商品单价,下单时间

SELECT * from customers61;
SELECT * from products61;
SELECT * from orders61;


-- where 内连接
SELECT * from orders61,products61,customers61
WHERE orders61.c_id = customers61.c_id 
AND orders61.p_id = products61.p_id;


SELECT * from orders61
inner JOIN customers61
ON orders61.c_id = customers61.c_id
INNER JOIN products61
ON orders61.p_id = products61.p_id;

image.png

-- 查有成绩的学生信息   (学生信息,student) (成绩,score) ==》 关系是s_id 内连接
  select student.* from student,score
    where student.s_id = score.s_id;

-- 查询「李」姓老师的数量

SELECT count(t_name) from teacher
WHERE t_name like "李%";

-- 查询名字中含有「风」字的学生信息
select * from student
WHERE s_name like "%风%";

-- 查询 1990 年出生的学生名单
SELECT * from student
WHERE s_birth like "1990%";

-- 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
-- 1. 查看张三老师教的课程id
SELECT t_id from teacher
WHERE t_name = "张三";  -- t_id   01
-- 2. 查看张三所教授的课程
select c_id from course  
WHERE t_id = "01";     -- 数学, 课程id c_id = 02

-- 3. 查看选修课为数学的学员信息  学生表,成绩表

select max(s_score) from score
WHERE c_id = "02";  -- 找到数学最高分  90

-- 4. 查找数学90分的学号
SELECT * from score
WHERE s_score = 90; -- 01

-- 4. 根据学号 查找学生
select S1.s_id,S1.s_name,S1.s_birth,S1.s_sex,score.c_id,score.s_score from 
(select * from student
WHERE s_id in (
    SELECT s_id from score
    WHERE s_score = 90
)) as S1

INNER JOIN score 
on S1.s_id = score.s_id;
AND score.c_id = "02"

总结

Mysql -04 - 图15Mysql -04 - 图16