SQL關鍵字的執行順序

紙上得來終覺淺

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

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

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

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

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

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

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

[html] view plain copy
  1. select[distinct]  
  2. from  
  3. join(如left join)  
  4. on  
  5. where  
  6. group by  
  7. having  
  8. union  
  9. order by  
  10. limit  

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

[html] view plain copy
  1. from  
  2. on  
  3. join  
  4. where  
  5. group by  
  6. having  
  7. select  
  8. distinct  
  9. union  
  10. 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;

發佈了52 篇原創文章 · 獲贊 76 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章