標籤:卡爾 速度慢 查詢 聯合 ora 顯示 表示 sel nbsp
在西面內容中 car 和 nation 都表示 表名
1.無論 進階查詢還是簡單查詢 都用 select.. from..語句 from 後面 加表名 可以使一張表也可以是多張表 表和表之間用逗號隔開
2. 簡單查詢和進階查詢 不是 獨立的 進階查詢裡面 同樣可以用到 簡單查詢
3.簡單查詢與複雜查詢的聯絡: 簡單查詢裡面 後面的條件 未知時 需要用另一個 查詢來代替 這樣就變成了進階查詢
4.連結查詢 和 聯集查詢的區別:1. 連結查詢 串連兩張以上的表 輸出時 顯示在一張表裡 ; 聯集查詢 以另一張表為條件 只輸出 一張表的內容
聯集查詢時 注意 相關查詢 : 子查詢 和父查詢 的關係 父查詢 的條件 時子查詢 子查詢的條件同樣引用子查詢
2.相同點 關聯在一起時 表示必須建立外鍵關係;
一.連結查詢
1.連結查詢 對結果集列的擴充
select*from 表名
查詢多張表 查詢結果 在一張表中顯示
select * from info,nation #形成笛卡爾積 缺點 查詢速度慢(產生大量冗餘資料)
select * from 表1名, 表1名where 表1名.列名=表2名.列名
select * from info,nation where info.code=nation.code
select info.code, info. name, birthday from info,nation where info.code=nation.code
因為 birthday 在兩張表裡 沒有重複 所以可以直接寫 (比如簡單查詢裡面)
select * from info join nation on info.nation=nation.code
二.聯集查詢 union
select 列名,列名 from 表1名
union
select 列名,列名 from 表2名
三. 子查詢(查詢效率高 重要)
父查詢 : 外層查詢
子查詢: 裡層查詢
子查詢 查詢出的結果作為父查詢的條件
- 無關子查詢
子查詢和父查詢沒有關係 子查詢可以單獨執行
父查詢:select *from info where nation=()
子查詢: select code from nation where name=’漢族’
select *from info where nation=(select code from nation where name=’漢族’)
②.查詢系列名為‘寶馬5系’的所有汽車資訊
select * from car where brand=(select brand_code from brand where brand_name=‘寶馬5系‘)
- 相互關聯的子查詢
子查詢在執行的時候 和父查詢有關係 子查詢不能單獨執行
1.查詢汽車表中 油耗小於平均油耗的所有汽車資訊
父查詢: 汽車的資訊: select * from car where oil<平均油耗
子查詢: 平均油耗; select avg(oil) from car where brand =該系列
select * from car as a where oil<(select avg(oil) from car as b where b.brand=a.brand)
Mysql 基礎 進階查詢