Mysql 基礎 進階查詢

來源:互聯網
上載者:User

標籤:卡爾   速度慢   查詢   聯合   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名

三.  子查詢(查詢效率高 重要)

父查詢 : 外層查詢  

子查詢: 裡層查詢

子查詢 查詢出的結果作為父查詢的條件

 

  1. 無關子查詢  

子查詢和父查詢沒有關係  子查詢可以單獨執行

父查詢: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. 相互關聯的子查詢

子查詢在執行的時候 和父查詢有關係   子查詢不能單獨執行

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 基礎 進階查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.