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的用法還有很多,以後再總結其他的。溫故而知新,知識就是要不斷的學習,不斷的複習,才能記得更加的深刻。不斷奮鬥,成就美好人生。人生的沿途都有美妙的風景,加油!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章