mysql数据库基本使用语法(左连接、右连接复杂查询)

多表关联查询sql语句,平常开发工作中经常使用,熟练sql的使用,可以提高我们的工作效率。在面试中经常会问到多表设计及查询。今天,就总结一下它的基本用法吧。

首先创建三张表,分别为学生表、成绩表、课程表,创建三个表的sql语句如下:

create table score (
           studentId VARCHAR(10),
           courseId VARCHAR(10),
           scores INT
       );
      create table student (
          studentId VARCHAR(10),
          studentName VARCHAR(20),
          studentSex VARCHAR(5), 
          studentAge INT 
     );
     CREATE TABLE course(
           courseId VARCHAR(10),
          courseName VARCHAR(20)
     );

三个表的测试数据分别如下:

 student:

score:

course:

1:查询小明的各科成绩:

       SELECT st.studentId,st.studentName,co.courseName,sc.scores FROM student st
               LEFT JOIN score sc ON st.studentId = sc.studentId
               LEFT JOIN course co ON sc.courseId = co.courseId
       WHERE st.studentName = '小明';

查询结果图如下:

2:查询小明所有成绩中的最高分:

SELECT st.studentId,st.studentName,co.courseName,max(sc.scores) FROM student st
               LEFT JOIN score sc ON st.studentId = sc.studentId
               LEFT JOIN course co ON sc.courseId = co.courseId
       WHERE st.studentName = '小明';

       查询结果图如下:

3:查询姓名中含有“小”字的学生信息:

SELECT st.studentId,st.studentName,sc.scores,co.courseName FROM score sc
               left  JOIN course co on sc.courseId = co.courseId 
               left JOIN student st on st.studentId = sc.studentId
       where st.studentName like '%小%' ;

       查询结果图如下:

 

4:查询小明所有成绩降序排列(DESC为降序,ASC为升序):

SELECT st.studentId,st.studentName,co.courseName,sc.scores FROM student st
               LEFT JOIN score sc ON st.studentId = sc.studentId
               LEFT JOIN course co ON sc.courseId = co.courseId
       WHERE st.studentName = '小明' ORDER BY sc.scores DESC;

       查询结果图如下:

5:查询每门课程的最高分学生姓名及学号:

SELECT st.studentId,st.studentName,co.courseName,sc.scores FROM student st
               LEFT JOIN score sc ON st.studentId = sc.studentId
               LEFT JOIN course co ON sc.courseId = co.courseId
       GROUP BY co.courseId HAVING max(sc.scores);

       查询结果图如下:

6:查询成绩在90-100分之间的学生信息:

SELECT st.studentId,st.studentName,co.courseName,sc.scores FROM student st
LEFT JOIN score sc ON st.studentId = sc.studentId
LEFT JOIN course co ON sc.courseId = co.courseId
where sc.scores BETWEEN 90 and 100;

查询结果图如下:

7:查询选修了语文课程的学生信息:

SELECT st.studentId,st.studentName,co.courseName,sc.scores FROM student st
               LEFT JOIN score sc ON st.studentId = sc.studentId
               LEFT JOIN course co ON sc.courseId = co.courseId
       where co.courseName = '语文';

查询结果图如下:

8:查询语文课程成绩最高的学生信息:

SELECT st.studentId,st.studentName,co.courseName,max(sc.scores) FROM student st
               LEFT JOIN score sc ON st.studentId = sc.studentId
               LEFT JOIN course co ON sc.courseId = co.courseId
       where co.courseName = '语文';

查询结果图如下:

以上总结了多表关联查询的基本用法,综上所述,主要有以下几点:

1:多表左连接关键字:left  join  ...on   

2:where后面不能跟聚合函数,如果使用聚合函数,用having;

3:DESC为降序排列,ASC为升序排列;

4:最大值函数为max(),最小值函数为min();

5:group  by和having关键字一起使用时,group by在前,having在后。

6:连续的数值之间用关键字between;

7:模糊查询时使用关键字like;

8:多表关联查询某个名称时尽可能使用子查询。

sql的用法还有很多,以后再总结其他的。温故而知新,知识就是要不断的学习,不断的复习,才能记得更加的深刻。不断奋斗,成就美好人生。人生的沿途都有美妙的风景,加油!

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值