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屬於多表查詢,放在最后展示)
語句一:
- select a.Customer
- from orders a
- where a.Customer='Bush' or a.Customer = 'Adams'
分析一:首先是from語句找到表格,然后根據where得到符合條件的記錄,最后select出需要的字段,結果如下:
語句二groupby:groupby要和聚合函數一起使用
- 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:
- 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)
- 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的結果進行合並(默認去掉重復的記錄):
- 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:
- 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
分析七:取出結果中的前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
分析八:上述語句其實join on就是多連接了一張表,而且是兩張一樣的表,都是Orders。 執行過程是,在執行from關鍵字之后根據on指定的條件,把left join指定的表格數據附在from指定的表格后面,然后再執行where字句。
注:
1)使用distinct要寫在所有要查詢字段的前面,后面有幾個字段,就代表修飾幾個字段,而不是緊隨distinct的字段;
2)group by執行后(有聚合函數),group by后面的字段在結果中一定是唯一的,也就不需要針對這個字段用distinct;
轉載:https://blog.csdn.net/jintao_ma/article/details/51253356