SQL statement is the basis - multi-table join query

Introduction join query

Join query including inner, left outer, right outer join, full outer connections and cross-connect, even a little later use, this only describes the connector and around the outer connector.
The following table as an example of a single-table query:
Stduent

Sno come off Shseksh Sbirthday Sdept Memo

Course

Who are Cname PreCno Credit Semester

SC

Sno Who are Grade

Specific data will not show up, as long as it can be queried according to the head of the table.

En

If the two tables of the relevant fields satisfies the condition, from the two tables are connected to extract data satisfying the conditions and combined into a new record, discard set condition is not satisfied
syntax :FROM TABLE 1 JOIN Table 2 ON <join condition>
<Connection condition> general format: a column name table 1. <Comparison operators> 2 column name table 2.
Note that the connection conditions listed for comparison must be the same, i.e., the column must be the same semantics .
example:

//查看选修了数据库的学生的学号和姓名
SELECT * FROM Student INNER JOIN SC
ON Student.Sno=SC.Sno;
//统计计算机系学生中每门课程的选课人数,平均分,最高分和最低分
SELECT Cno,COUNT(*) 选课人数,AVG(Grade) 平均分,
MAX(Grade) 最高分,MIN(Grade) 最低分,
FROM Student S JOIN SC ON S.Sno=SC.Sno

Since the connection
from the connector is a special connection, it refers to physically the same table, but it logically connected as two tables, a table alias must connect from the note
syntax:
FROM TABLE 1 AS T1 JOIN Table 1 AS T2 ON T1 column name = T2. Listed
E.g:

//查询与“钟文辉”同学同在一个系的学生的姓名和所在系
SELECT S2,Sname,S1,Sdept
FROM Student S1 JOIN Student S2 ON S2.Sdept=S1.Sdept
WHERE S1.Sname='钟文辉' AND S2.Sname!='钟文辉'

Left outer join, right outer join

When connected, the connection does not satisfy the conditions of reservations, combination table corresponding positions NULL to left outer left table is retained, the right is reserved outside the right table.
Syntax
FROM TABLE 1 LEFT | RIGHT [OUTER] JOIN Table 2 ON <join condition>
E.g:

//查询没有人选的课程的课程名
SELECT Cname,Sno FROM Course C LEFT 
JOIN SC ON C,Cno=SC.Cno
WHERESC.Cno IS NULL;

Guess you like

Origin blog.csdn.net/livovil/article/details/93650401