天天看點

mysql 四表連結查詢_MySQL多表連接配接查詢一

MySQL多表查詢一

示例一:

查詢"1234"課程比"1235"課程成績高的學生的資訊及課程分數

分析:學生資訊在student表,成績資訊在student_course表,同時要比較student_course表中1234課程和1235課程成績的大小,還要顯示其成績

是以,要三表連接配接查詢

mysql> select a.* , b.* ,c.*

from student a , student_course b , student_course c;

加上查詢條件

mysql> select a.* , b.score as 1234score ,c.score 1235score

from student a , student_course b , student_course c

where a.sid = b.sid and a.sid = c.sid and b.cid = '1234' and c.cid = '1235' and c.score > b.score;

+-----+-------+---------------------+------+-----------+-----------+

| sid | sname | sage                | ssex | 1234score | 1235score |

+-----+-------+---------------------+------+-----------+-----------+

| 01  | 趙一  | 1990-01-01 00:00:00 | 男   |        10 |        90 |

| 02  | 趙二  | 1990-01-01 00:00:00 | 男   |        70 |        90 |

+-----+-------+---------------------+------+-----------+-----------+

2 rows in set

最後就是這個結果

示例二:

student a , student_course b兩表連接配接查詢

查詢所有同學的學生編号、學生姓名、選課總數、所有課程的總成績

mysql> select a.sid 學生編号, a.sname 學生姓名, count(b.cid) 選課總數, sum(score) 所有課程的總成績

from student a , student_course b

where a.sid = b.sid

group by a.sid,a.sname

order by a.sid;

+----------+----------+----------+------------------+

| 學生編号 | 學生姓名 | 選課總數 | 所有課程的總成績 |

+----------+----------+----------+------------------+

| 01       | 趙一     |        2 |              100 |

| 02       | 趙二     |        2 |              160 |

| 03       | 趙三     |        2 |              146 |

+----------+----------+----------+------------------+

3 rows in set

mysql>

示例三:

兩表的左外連接配接

查詢所有(包括有成績和無成績)的SQL

mysql> select a.sid 學生編号, a.sname 學生姓名, count(b.cid) 選課總數, sum(score) 所有課程的總成績

from Student a left join student_course b

on a.sid = b.sid

group by a.sid,a.sname

order by a.sid;

+----------+----------+----------+------------------+

| 學生編号 | 學生姓名 | 選課總數 | 所有課程的總成績 |

+----------+----------+----------+------------------+

| 01       | 趙一     |        2 |              100 |

| 02       | 趙二     |        2 |              160 |

| 03       | 趙三     |        2 |              146 |

| 04       | 趙四     |        0 | NULL             |

+----------+----------+----------+------------------+

4 rows in set

示例四:

四表連接配接查詢

查詢上xx老師課的學生的資訊

mysql> select distinct student.* from student , student_course , course , teacher

where student.sid = student_course.sid and student_course.cid = course.cid and course.tid = teacher.tid and teacher.tname = '李四'

order by student.sid;

+-----+-------+---------------------+------+

| sid | sname | sage                | ssex |

+-----+-------+---------------------+------+

| 01  | 趙一  | 1990-01-01 00:00:00 | 男   |

| 02  | 趙二  | 1990-01-01 00:00:00 | 男   |

+-----+-------+---------------------+------+

2 rows in set

示例五:

和示例四一樣,這個用join來實作

mysql> select distinct a.*

from student a join student_course b on a.sid = b.sid

join course c on b.cid = c.cid join teacher d on c.tid = d.tid

where d.tname = '李四'

order by a.sid;

+-----+-------+---------------------+------+

| sid | sname | sage                | ssex |

+-----+-------+---------------------+------+

| 01  | 趙一  | 1990-01-01 00:00:00 | 男   |

| 02  | 趙二  | 1990-01-01 00:00:00 | 男   |

+-----+-------+---------------------+------+

2 rows in set

====更新中====