紙上得來終覺淺
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
建立如下表格orders:
注:下面所有語句符合語法順序(也不可能不符合,因爲會報錯^_^),只分析其執行順序:(join和on屬於多表查詢,放在最後展示)
語句一:
- select a.Customer
- from orders a
- where a.Customer='Bush' or a.Customer = 'Adams'
語句二groupby:groupby要和聚合函數一起使用
- select a.Customer,sum(a.OrderPrice)
- from orders a
- where a.Customer='Bush' or a.Customer = 'Adams'
- group by a.Customer
語句三having:
- 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
語句四distinct: (爲測試,先把數據庫中Adams那條記錄的OrderPrice改爲3000)
- 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
語句五union:完全是對select的結果進行合併(默認去掉重複的記錄):
- 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:
- 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:
- 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
語句八(上面基本講完,下面是join 和 on):
- 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
注:
1)使用distinct要寫在所有要查詢字段的前面,後面有幾個字段,就代表修飾幾個字段,而不是緊隨distinct的字段;
2)group by執行後(有聚合函數),group by後面的字段在結果中一定是唯一的,也就不需要針對這個字段用distinct;