標籤: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
- select a.Customer
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘
分析一:首先是from語句找到表格,然後根據where得到合格記錄,最後select出需要的欄位,結果如下:
語句二groupby:groupby要和彙總函式一起使用
[html] view plain copy
- select a.Customer,sum(a.OrderPrice)
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘
- group by a.Customer
分析二:在from,where執行後,執行group by,同時也根據group by的欄位,執行sum這個彙總函式。這樣的話得到的記錄對group by的欄位來說是不重複的,結果如下:
語句三having:
[html] view plain copy
- select a.Customer,sum(a.OrderPrice)
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘
- group by a.Customer
- having sum(a.OrderPrice) > 2000
分析三:由於where是在group之前執行,那麼如何對group by的結果進行篩選,就用到了having,結果如下:
語句四distinct: (為測試,先把資料庫中Adams那條記錄的OrderPrice改為3000)
[html] view plain copy
- select distinct sum(a.OrderPrice)
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 1700
分析四:將得到一條記錄(沒有distinct,將會是兩條同樣的記錄):
語句五union:完全是對select的結果進行合并(預設去掉重複的記錄):
[html] view plain copy
- select distinct sum(a.OrderPrice) As Order1
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 1500
- union
- select distinct sum(a.OrderPrice) As Order1
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 2000
分析五:預設去掉重複記錄(想保留重複記錄使用union all),結果如下:
語句六order by:
[html] view plain copy
- select distinct sum(a.OrderPrice) As order1
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 1500
- union
- select distinct sum(a.OrderPrice) As order1
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 2000
- order by order1
分析:升序排序,結果如下:
語句七limit:
[html] view plain copy
- select distinct sum(a.OrderPrice) As order1
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 1500
- union
- select distinct sum(a.OrderPrice) As order1
- from orders a
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 2000
- order by order1
- limit 1
分析七:取出結果中的前1條記錄,結果如下:
語句八(上面基本講完,下面是join 和 on):
[html] view plain copy
- select distinct sum(a.OrderPrice) As order1,sum(d.OrderPrice) As order2
- from orders a
- left join (select c.* from Orders c) d
- on a.O_Id = d.O_Id
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 1500
- union
- select distinct sum(a.OrderPrice) As order1,sum(e.OrderPrice) As order2
- from orders a
- left join (select c.* from Orders c) e
- on a.O_Id = e.O_Id
- where a.Customer=‘Bush‘ or a.Customer = ‘Adams‘ or a.Customer = ‘Carter‘
- group by a.Customer
- having sum(a.OrderPrice) > 2000
- order by order1
- limit 1
分析八:上述語句其實join on就是多串連了一張表,而且是兩張一樣的表,都是Orders。 執行過程是,在執行from關鍵字之後根據on指定的條件,把left join指定的表格式資料附在from指定的表格後面,然後再執行where字句。
註:
1)使用distinct要寫在所有要查詢欄位的前面,後面有幾個欄位,就代表修飾幾個欄位,而不是緊隨distinct的欄位;
2)group by執行後(有彙總函式),group by後面的欄位在結果中一定是唯一的,也就不需要針對這個欄位用distinct;
Mysql 語句執行順序