子查詢:
在一個 select 語句中,嵌入了另外一個 select 語句, 那么被嵌入的 select 語句稱之為子查詢語句
主查詢和子查詢的關系:
子查詢是嵌入到主查詢中,子查詢是輔助主查詢的,要么充當條件,要么充當數據源,子查詢是可以獨立存在的語句,是一條完整的 select 語句
子查詢分類:
標量子查詢: 子查詢返回的結果是一個數據(一行一列) 一般使用的是比較運算符
select * from student where age > (select avg(age) from student);
列子查詢: 返回的結果是一列(一列多行) 一般使用in
select * from student where tid in (select id from teacher);
行子查詢: 返回的結果是一行(一行多列) 需求: 查找班級年齡最大,身高最高的學生
行元素: 將多個字段合成一個行元素,在行級子查詢中會使用到行元素
select * from student where (height,age) = (select max(height),max(age) from student);
執行順序為:
from 表名
where ....
group by ...
select distinct *
having ...
order by ...
limit start,count
實際使用中,只是語句中某些部分的組合,而不是全部
連接查詢:
select 字段 from 表名1 inner join 表名2 on 關聯條件
內連接查詢:查詢的結果為兩個表匹配到的數據
select 字段 from 表名1 right join 表名2 on 關聯條件
右連接查詢:查詢的結果為兩個表匹配到的數據,右表特有的數據,對於左表中不存在的數據使用null填充
select 字段 from 表名1 left join 表名2 on 關聯條件
左連接查詢:查詢的結果為兩個表匹配到的數據,左表特有的數據,對於右表中不存在的數據使用null填充