高級查詢:
一:多表連接
1.select Info.Code,Info.Name,Nation.Name from Info,Nation where Info.Nation = Nation.Code 查幾張表就就輸出幾張表,查那個條件就輸出那個條件 列的查詢
select * from Info,Nation 全部輸出4x4
2.join連接
select * from Info join Nation on Info.Nation = Nation.Code 篩選輸出數據
二:多表聯合
select * from Info where Code='p001'union select * from Info where Nation='n001' union 聯合 行的查詢
三:子查詢(無關子查詢)
select * from Info where Nation = (select Code from Nation where Name='漢族') 兩個查詢 一個查詢的結果當做另一個查詢的條件 查一個 =
select * from Info where Nation in (select Code from Nation where Name='漢族' or Name='苗族')
in(在里面)not in (在不里面)任意一個都可以 作為兩個查詢結果的鏈接 查兩個in
四:子查詢(相關子查詢)
select * from Car a where a.Oil <(select avg(Oil) from Car b where b.Brand = a.Brand)
把外子查詢定義一個a 里面的表定義成b 外層表看油耗 里層求油耗的平均值(每一個數據都走一遍)