MySQL基礎知識 查詢

1、DBMS與數據庫,DBMS是操縱數據庫的軟件,數據庫是存放數據的容器,MySQL是一種DBMS

2、模式(scheme):關於數據庫和表的佈局和特性信息

3、基於共享文件系統的DBMS:Access、FileMaker;基於客戶端和服務器的DBMS:MySQL、Oracle、SQLServer

4、MySQL命令行:用;或者\g標誌語句結束,僅僅按enter鍵,不會執行語句;quit、exit用於退出命令行程序

5、選擇數據庫 use database_name\g 返回database changed

6、查看數據庫情況 show databases\g 返回所有數據庫

7、show tables\g 返回當前數據所有表

8、show columns from database_name\g 返回表中的所有列相關信息,這一條命令的快捷方式describe,比如describe database_name\g

9、select distinct column_name from database_name\g 返回列中所有不同的值,distinct用於所有的列,而不是它所前置的列。

10、select column_name from table_name limit 1,1\g 返回第二行

       select column_name from table_name limit 5\g 返回前五行

11、完全限定(表名和列名)select table_name.column from database_name.table_name

12、order by 完全可以用非檢索的列排序

select table_name.column_name_1, table_name.column_name_2 
from database_name.table_name
order by table_name.column_name_3\g

13、按照多個列排序,order by後的各個列,用‘,’分開;表示第一個拍序列相同的情況下,用第二個列排序。

select table_name.column_name_1, table_name.column_name_2 
from database_name.table_name
order by table_name.column_name_3, table_name.column_name_4\g

14、按照降序排列和按照升序排列 DESC和ASC(默認的,所以一般不用);DESC只作用於其前面的哪一個排序列

select table_name.column_name_1, table_name.column_name_2 
from database_name.table_name
order by table_name.column_name_3 DESC, table_name.column_name_4\g

15、order by 和 limit組合作用,獲得最大值和最小值(注意order by必須放在from,where後面,limit必須放在order by後面)

select id,price,name        //檢索 交易id 價格 買家名稱
from trade                  //交易表
order by price desc,name    //價格降序,名稱升序
limit 5 \g                  //最貴的5筆交易

16、where字句支持的操作符


17、BETWEEN AND的用法

select price, name from trade where price BETWEEN5 AND10\g

18、判斷是否爲空 is null字句 的用法

select price, name from trade where price  IS NULL\g

19、在進行匹配過濾(where column_name = '1')不匹配過濾(where column_name != '2')時,column_name列的值爲null的行都得不到返回,因爲數據庫不知道null值是否跟他們匹配。尤其是使用不匹配過濾的時候,可能會希望返回該列爲null 的值,所以一定要注意。

20、AND 和 OR 的計算次序問題,and的優先級高於or,必要時可以添加“()”

21、關鍵字 IN 的用法    select price, name from trade where price IN (10,12,13,18)\g

       IN的優點:更直觀,能夠更好的組織計算次序;執行起來一般比OR更快;可以和select組合

22、NOT 關鍵字:對後跟條件取反,MySQL支持對IN、EXISTS 、BETWEEN取反,但是大多數其他的DBMS支持NOT對所有的後跟條件取反。

23、通配符‘%’的注意事項:1、需要與like聯用;2、可以匹配0、1、多個字符;3、不能匹配NULL;4、注意尾部有空格的字符串,比如“hello ”尾部有一個空格,“%llo”則不能匹配,解決方法是:用“%llo%”匹配或者用函數去掉尾部空格。

24、通配符‘_’只匹配一個字符

25、注意事項,通配符較其他搜索方式速度較慢,儘量少用;儘量不要把通配符使用在搜索模式的開始處(如“%ol”),那樣最慢。

26、正則表達式,見 正則表達式篇

27、拼接字段,多數DBMS使用‘+’或者‘||’實現拼接,而MySQL使用 Concat() 函數,SQL語句轉換成MySQL語句的時候請注意。使用:    select Concat(name,'(',country,')') from person order by name\g

28、Trim()、RTrim()、LTrim()函數。select Concat(RTrim(name),'(',RTrim(country),')') from person order by name\g

29、別名(導出列)。select Concat(RTrim(name),'(',RTrim(country),')') AS title from person order by name\g。別名用於對原有列名稱的補足,比如原有列名具有不合法的字符。

30、算數計算,支持四則遠算,支持利用小括號改變優先級

31、select Now()返回當前的日期和時間

32、分組的注意事項

  • group by後可以跟多個列
  • group by字句中列出的每個列,必須是檢索列,或者是有效的表達式,但是不能是聚集函數,select後跟的表達式,也必須出現在group by後面,不能使用別名
  • select後跟的每一個列必須在group by字句中給出
  • 如果分組列中具有NULL,他們會被分爲一組
  • group by出現在where之後,order by之前

33、使用having子句過濾分組:where相當於是在分組之前進行過濾,having相當於對分組進行過濾,having 不能使用別名

示例:具有兩種以上價格爲10以上的供應商id

select vent_id,count(*)              //供應商id,分組統計
from product_info                    //商品表
where prod_price>10                  //商品價格>10
group by vent_id                     //對供應商進行分組
having count(*)>=2                   //分組統計>=2

示例:總訂單價格大於等於50元的訂單號和總訂單價格

select order_num,sum(item_price*item_num) as ordered_total
from prod_ordered_info
group by order_num
having sum(item_price*item_num)>=50
order by ordered_total

34、select子句的順序



35、子查詢:

示例1(利用子查詢進行過濾):假設有三張表,guest_info,order_info,order_item_info分別存儲,客戶信息,訂單信息,訂購商品信息,查詢訂購了‘candy’的所有用戶名稱

select order_num from order_item_info where prod_id = 'candy'\g

select guest_id from order_info where order_num in ...\g

select guest_id,guest_name from guest_info where guest_id in ...\g

答案:

select guest_id,guest_name 

from guest_info 

where guest_id in (  select guest_id 

                                from order_info 

                                where order_num in ( select order_num 

                                                                    from order_item_info 

                                                                    where prod_id = 'candy'))\g

示例2(作爲計算字段使用子查詢):統計所有的客人的各自訂單數

select count(*) from order_info where guest_info.guest_id = order_info.guest_id\g

select guest_name,guest_contact,(...) from guest_info\g

答案:

select guest_name,guest_contact,( select count(*) 

                                                        from order_info 

                                                        where guest_info.guest_id = order_info.guest_id)  as orders

from guest_info

order by orders\g

36、等值連接(內部鏈接)

一般用法:

select c.cust_id o.order_num

from cust_info as c inner join order_info as o

on c.cust_id = o.cust_id

示例:用內聯結的方法解決第35條的示例一的問題

select guest_name,guest_contact

from guest_info,order_info,order_item_info

where guest_info.guest_id=order_info.guest_id and

           order_info.order_num=order_item_info.order_num and

           order_item_info.prod_id='candy'\g

37、自聯結

示例:prod_id = 'candy'的商品出現了問題,需要看看它的生產廠家生產的其他商品是否有問題,如何查詢:

方法一,使用子查詢 

select vend_id from prod_info where prod_id='candy'\g

select prod_id,prod_name from prod_info where vend_id=...\g

結果:

select prod_id,prod_name 

from prod_info 

where vend_id=(    select vend_id 

                               from prod_info

                               where prod_id='candy')\g

方法二:使用自聯結

select a.prod_id,b.prod_name 

from prod_info as a,prod_info as b

where a.vend_id=b.vend_id and

           b.prod_id='candy'\g

38、自然聯結:手動排除因爲表連接而產生的重複列,事實上我們可能永遠用不到非自然連接

39、外聯結

外連接的應用:列出所有的客戶和他們的訂單號,包括哪些沒有訂單的客戶

select c.cust_id o.order_num

from cust_info as c left out join order_info as o//外連接左表,也就是cust_info 表,所以會把cust_info 中的字段留全

on c.cust_id = o.cust_id

40、聚集函數與聯結

select c.cust_id,c.cust_name,count(order_num) as order_num

from cust_info as c left out join order_info as o

on c.cust_id=o.cust_id

group by cust_info.id

輸出了包含0個訂單的客戶和其統計

41、union 與 union all

有9條數據

有8條數據

注意到 order by只是對最終的數據集排序

42、全文搜索的列,在簡歷表的時候,必須加上全文索引;搜索示例如下:

select note_text from productnotes where match(note_text) against('rabbit')\g

注:傳遞給match的值,必須與FULLTEXT()定義中的相同,若指定多個列,則必須列出他們,而且次序相同

       全文搜索可以對結果自動排序










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章