Mysql 語句執行順序

來源:互聯網
上載者:User

標籤:tools   left join   學習   idt   strong   技術   ada   XML   code   

轉載自:http://blog.csdn.net/jintao_ma/article/details/51253356

1.這樣一個問題,作為一個開發人員需要掌握資料庫的哪些東西?  在開發中涉及到資料庫,基本上只用到了sql語句,如何寫sql以及對其進行最佳化就比較重要,那些mysql的厚本書籍針對的是DBA,我們只需要學習其中的sql就可以了。

2.既然會寫sql是目標,那麼怎麼才能寫好sql.學習下面幾點:

1)Mysql的執行順序,這個是寫sql的核心,之前遇到的一些錯誤就是因為對其不瞭解;

2)如何進行多表查詢,最佳化,這個是很重要的部分;

3)sql語句的函數,sql提供的函數方便了很多操作;

3.這篇對Mysql語句執行順序的學習做了總結:

1)Mysql文法順序,即當sql中存在下面的關鍵字時,它們要保持這樣的順序:

select[distinct]  from  join(如left join)  on  where  group by  having  union  order by  limit  

  

2)Mysql執行順序,即在執行時sql按照下面的順序進行執行:

from  on  join  where  group by  having  select  distinct  union  order by   

3)針對上面的Mysql文法順序和執行順序,循序漸進進行學習:

建立如下表格orders:

註:下面所有語句符合文法順序(也不可能不符合,因為會報錯^_^),只分析其執行順序:(join和on屬於多表查詢,放在最後展示)

語句一:

 

[html] view plain copy  
  1. select a.Customer  
  2. from orders a  
  3. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘  

分析一:首先是from語句找到表格,然後根據where得到合格記錄,最後select出需要的欄位,結果如下:

 


語句二groupby:groupby要和彙總函式一起使用

 

[html] view plain copy  
  1. select a.Customer,sum(a.OrderPrice)  
  2. from orders a  
  3. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘  
  4. group by a.Customer  

分析二:在from,where執行後,執行group by,同時也根據group by的欄位,執行sum這個彙總函式。這樣的話得到的記錄對group by的欄位來說是不重複的,結果如下:

 

語句三having:

 

[html] view plain copy  
  1. select a.Customer,sum(a.OrderPrice)  
  2. from orders a  
  3. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘  
  4. group by a.Customer  
  5. having sum(a.OrderPrice) > 2000  

分析三:由於where是在group之前執行,那麼如何對group by的結果進行篩選,就用到了having,結果如下:

 

語句四distinct: (為測試,先把資料庫中Adams那條記錄的OrderPrice改為3000)

 

[html] view plain copy  
  1. select distinct sum(a.OrderPrice)  
  2. from orders a  
  3. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  4. group by a.Customer  
  5. having sum(a.OrderPrice) > 1700  

分析四:將得到一條記錄(沒有distinct,將會是兩條同樣的記錄):

 

語句五union:完全是對select的結果進行合并(預設去掉重複的記錄):

 

[html] view plain copy  
  1. select distinct sum(a.OrderPrice) As Order1  
  2. from orders a  
  3. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  4. group by a.Customer  
  5. having sum(a.OrderPrice) > 1500  
  6. union  
  7. select distinct sum(a.OrderPrice) As Order1  
  8. from orders a  
  9. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  10. group by a.Customer  
  11. having sum(a.OrderPrice) > 2000  

分析五:預設去掉重複記錄(想保留重複記錄使用union all),結果如下:

 

語句六order by:

 

[html] view plain copy  
  1. select distinct sum(a.OrderPrice) As order1  
  2. from orders a  
  3. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  4. group by a.Customer  
  5. having sum(a.OrderPrice) > 1500  
  6. union  
  7. select distinct sum(a.OrderPrice) As order1  
  8. from orders a  
  9. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  10. group by a.Customer  
  11. having sum(a.OrderPrice) > 2000  
  12. order by order1  

分析:升序排序,結果如下:

 

語句七limit:

 

[html] view plain copy  
  1. select distinct sum(a.OrderPrice) As order1  
  2. from orders a  
  3. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  4. group by a.Customer  
  5. having sum(a.OrderPrice) > 1500  
  6. union  
  7. select distinct sum(a.OrderPrice) As order1  
  8. from orders a  
  9. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  10. group by a.Customer  
  11. having sum(a.OrderPrice) > 2000  
  12. order by order1  
  13. limit 1  

分析七:取出結果中的前1條記錄,結果如下:

 


語句八(上面基本講完,下面是join 和 on):

 

[html] view plain copy  
  1. select distinct sum(a.OrderPrice) As order1,sum(d.OrderPrice) As order2  
  2. from orders a  
  3. left join (select c.* from Orders c) d   
  4. on a.O_Id = d.O_Id  
  5. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  6. group by a.Customer  
  7. having sum(a.OrderPrice) > 1500  
  8. union  
  9. select distinct sum(a.OrderPrice) As order1,sum(e.OrderPrice) As order2  
  10. from orders a  
  11. left join (select c.* from Orders c) e   
  12. on a.O_Id = e.O_Id  
  13. where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘  
  14. group by a.Customer  
  15. having sum(a.OrderPrice) > 2000  
  16. order by order1  
  17. limit 1  

分析八:上述語句其實join on就是多串連了一張表,而且是兩張一樣的表,都是Orders。 執行過程是,在執行from關鍵字之後根據on指定的條件,把left join指定的表格式資料附在from指定的表格後面,然後再執行where字句。

註:

1)使用distinct要寫在所有要查詢欄位的前面,後面有幾個欄位,就代表修飾幾個欄位,而不是緊隨distinct的欄位;

2)group by執行後(有彙總函式),group by後面的欄位在結果中一定是唯一的,也就不需要針對這個欄位用distinct;

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.