記錄
,從技術上講:行纔是正確的術語;http://www.javashuo.com/article/p-bvuuljcg-nv.htmlhtml
選擇數據庫:mysql
#顯示數據庫 show databases; #使用數據庫 use DatabaseName;
mysql> show databases; +----------------------+ | Database | +----------------------+ | information_schema | | MySQL_Crash_Course | | MySQL_Crash_Course_1 | | demo | | mysql | | performance_schema | | sys | +----------------------+ 7 rows in set (0.00 sec) mysql> use MySQL_Crash_Course; Database changed
瞭解數據庫和表git
返回當前選擇的數據庫內可用表的列表(show tables;)正則表達式
#顯示錶名 show tables; mysql> show tables; +-------------------------+ | Tables_in_MySQL_Student | +-------------------------+ | customers | | orderitems | | orders | | productnotes | | products | | vendors | +-------------------------+ 6 rows in set (0.00 sec)
show columns from customers
解析:sql
檢索單個列:數據庫
select prod_name from products;
注:編程
若是沒有明確的排序查詢結果,則返回的數據的順序是沒有特殊意義的;segmentfault
返回數據的順序多是數據被添加到表中的順序,也可能不是;markdown
只要返回的想用數目的行就是正常的。編程語言
檢索多個列:
select prod_id,prod_name,prod_price from products;
注:
檢索全部列:
select * from products;
注:
檢索不一樣的行:
關鍵字:DISTINCT--->必須放到列名的前面
select DISTINCT vend_id from products;
注:
限制結果:
關鍵字:LIMIT
select prod_name from products LIMIT 5;
注:
select prod_name from products LIMIT 5,5;
注:
使用徹底限定的表名:
#同時使用表名和列名 select products.prod_name from products;
#假定products確實位於crashcourse數據庫中 select products.prod_name from crashcourse.products;
日常進行查詢的話是沒有特定的順序,
select prod_name from products;
檢索的數據並非純粹的隨機順序顯示的,數據後來進行過更新或者刪除,則此順序將會收到MySQL重用回收存儲空間的影響
注:若是不明確規定排序順序,則不該該假定檢索出的數據順序有意義。
關鍵字:order by---取出一個或多個列的名字,據此對輸出進行排序;
select prod_name from products order by prod_name;
prod_name以字母順序排序數據
指定列名,列名之間用逗號分割;
select prod_id, prod_name,prod_price from products order by prod_price,prod_name;
首先按照價格排序,而後按照名字排序;
僅在多個行具備相同的prod_price值時纔對產品按照prod_name進行排序,若是prod_price列中全部的值都是惟一的,則不會按prod_name排序。
MySQL默認的排序時ASC(升序);
關鍵字:desc (降序)
select prod_id,prod_name,prod_price from products order by prod_price desc;
篩選出最貴的依次放在前面
select prod_id,prod_name,prod_price from products order by prod_price desc,prod_name;
只對prod_price有效;
找到最高/最低的值:order by+limit;
select prod_price from products order by prod_price desc limit 1 ;
關鍵字:where,where+搜索條件/過濾條件;
demo:
select prod_name,prod_price from products where prod_price =2.50;
同時使用where與order by時,應該order by在where以後,不然將會產生錯誤;
運算符 | 描述 |
---|---|
= | 等於 |
<> | 不等於。註釋:在 SQL 的一些版本中,該操做符可被寫成 != |
> | 大於 |
< | 小於 |
>= | 大於等於 |
<= | 小於等於 |
BETWEEN | 在某個範圍內 |
LIKE | 搜索某種模式 |
IN | 指定針對某個列的多個可能值 |
demo:
select prod_name,prod_proce from products where prod_name ='fuses'
例:列出價格小於10美圓的全部產品:
select prod_name,prod_price from products where prod_price<10;
列出價格小於等於10美圓的全部產品:
select prod_name,prod_price from products where prod_price<=10;
列:不是由供應商1003製造的全部產品
select vend_id,prod_name from products where vend_id <>1003;
select vend_id,prod_name from products where vend_id !=1003;
select vend_id,prod_name from products where not vend_id =1003;
關鍵字:between 開始值 and 結束值
例:檢索價格在5美圓和10美圓之間的全部產品
selecy prod_name,prod_price from products where prod_price between 5 and 10;
between關鍵字匹配的範圍中全部的值,包括指定的開始值與結束值;
在一個列中不包含值時,稱爲包含空值NULL;
NULL 無值,它與字段包含0,空字符串或者僅僅包含空格不一樣;
關鍵字:is null;檢查具備NULL的列
select prod_name from products where prod_price is null;
select cust_id from customers where cust_email is null;
在經過過濾選擇出不具備特定值的行時,你可能但願返回具備NULL值的行。可是,不行。由於未知具備特殊的含義,數據庫不知道它們是否匹配,因此在匹配過濾或不匹配過濾時不返回它們;
所以,再過濾數據時,必定要驗證返回數據中確實給出了被過濾列具備的NULL的行。
關鍵字:and or not in (括號)-->計算優先級
操做符:用來聯結或改變where子句中的子句的關鍵字,也成爲邏輯操做符
關鍵字:and 用來指示檢索知足全部給定條件的行;
select prod_id,prod_name,prod_price from products where vend_id =1003 and prod_price <=10;
注:還能夠添加多個過濾條件,每添加一個就要使用一個and;
關鍵字:or 檢索任意給定的條件,而不是同時匹配兩個或多個條件。
select prod_id,prod_name,prod_price from products where vend_id =1003 or vend_id =1002;
and在計算次序中優先級更高
列出價格爲10美圓以上而且由1002或者1003製造的全部產品:
select vend_id,prod_price,prod_name from products where vend_id =1002 or vend_id =1003 and prod_price >=10
輸出:
+---------+------------+----------------+ | vend_id | prod_price | prod_name | +---------+------------+----------------+ | 1003 | 13.00 | Detonator | | 1003 | 10.00 | Bird seed | | 1002 | 3.42 | Fuses | | 1002 | 8.99 | Oil can | | 1003 | 50.00 | Safe | | 1003 | 10.00 | TNT (5 sticks) | +---------+------------+----------------+
上面的輸出並無知足咱們的條件;緣由是;and的優先級高,因此執行的時候是(vend_id =1002)or(vend_id =1003 and prod_price >=10);而不是從左到右的方式執行;
整解:使用括號明確的分組
select vend_id,prod_price,prod_name from products where (vend_id =1002 or vend_id =1003) and prod_price >=10;
+---------+------------+----------------+ | vend_id | prod_price | prod_name | +---------+------------+----------------+ | 1003 | 13.00 | Detonator | | 1003 | 10.00 | Bird seed | | 1003 | 50.00 | Safe | | 1003 | 10.00 | TNT (5 sticks) | +---------+------------+----------------
不要過度的依賴默認的計算次序,使用圓括號沒有什麼壞處,他能消除歧義。
關鍵字:IN 用來指定條件範圍,範圍中的每一個條件均可以進行匹配
IN取合法值的由逗號分割的清單,所有包括在圓括號中;
select prod_name,prod_price from products where vend_id in(1002,1003) order by prod_name; +----------------+------------+ | prod_name | prod_price | +----------------+------------+ | Bird seed | 10.00 | | Carrots | 2.50 | | Detonator | 13.00 | | Fuses | 3.42 | | Oil can | 8.99 | | Safe | 50.00 | | Sling | 4.49 | | TNT (1 stick) | 2.50 | | TNT (5 sticks) | 10.00 | +----------------+------------+ 9 rows in set (0.00 sec)
IN操做符的做用與OR有相同的功能;
select prod_name,prod_price from products where vend_id=1002 or vend_id=1003 order by prod_name; +----------------+------------+ | prod_name | prod_price | +----------------+------------+ | Bird seed | 10.00 | | Carrots | 2.50 | | Detonator | 13.00 | | Fuses | 3.42 | | Oil can | 8.99 | | Safe | 50.00 | | Sling | 4.49 | | TNT (1 stick) | 2.50 | | TNT (5 sticks) | 10.00 | +----------------+------------+
IN操做符的好處:
在使用長的合法選項清單時,IN操做符的語法更清楚且更直觀。
在使用IN時,計算的次序更容易管理(由於使用的操做符更少)。
IN操做符通常比OR操做符清單執行更快。
IN的最大優勢是能夠包含其餘SELECT語句,使得可以更動態地創建WHERE子句。第11章將對此進行詳細介紹。
IN WHERE子句中用來指定要匹配值的清單的關鍵字,功能與OR至關。
關鍵字:not 用來否認後跟條件的關鍵字
例:列出除1002和1003以外的全部供應商製造的產品
select prod_name,prod_price from products where vend_id not in (1002,1003) order by prod_name;
+--------------+------------+ | prod_name | prod_price | +--------------+------------+ | .5 ton anvil | 5.99 | | 1 ton anvil | 9.99 | | 2 ton anvil | 14.99 | | JetPack 1000 | 35.00 | | JetPack 2000 | 55.00 | +--------------+------------+ 5 rows in set (0.00 sec)
關鍵字:LIKE 從技術上講,LIKE是謂詞而不是操做符;
通配符:用來匹配值的一部分的特殊字符;
搜索模式:由字面值、通配符或者二者組合構成的搜索條件;
LIKE指示MySQL後跟的搜索模式利用通配符匹配而不是直接相等匹配進行比較。
%表示任何字符出現的次數;
例:找出全部以jet開頭的產品
select prod_id,prod_name from products where prod_name like 'jet%';
+---------+--------------+ | prod_id | prod_name | +---------+--------------+ | JP1000 | JetPack 1000 | | JP2000 | JetPack 2000 | +---------+--------------+ 2 rows in set (0.00 sec)
通配符能夠在搜索模式中任意位置應用,而且可使用多個通配符
select prod_id,prod_name from products where prod_name like '%anvil%'; +---------+--------------+ | prod_id | prod_name | +---------+--------------+ | ANV01 | .5 ton anvil | | ANV02 | 1 ton anvil | | ANV03 | 2 ton anvil | +---------+--------------+ 3 rows in set (0.00 sec)
通配符也能夠出如今搜索模式的中間
mysql> select prod_id,prod_name from products where prod_name like 's%e'; +---------+-----------+ | prod_id | prod_name | +---------+-----------+ | SAFE | Safe | +---------+-----------+ 1 row in set (0.00 sec)
%除了一個或者多個字符外,還能夠匹配0個字符;%表明搜索模式中給定位置的0 個 1個或者多個字符;
注:尾空格可能會干擾通配符匹配(%anvil)
解決:在搜索模式後附加一個%,或者使用函數---11章介紹
注:可是通配符不能匹配NULL
做用:只能匹配單個字符而不是多個字符;
(_)
mysql> select prod_id,prod_name from products where prod_name like '_ ton anvil'; +---------+-------------+ | prod_id | prod_name | +---------+-------------+ | ANV02 | 1 ton anvil | | ANV03 | 2 ton anvil | +---------+-------------+ 2 rows in set (0.00 sec)
對照(%):
mysql> select prod_id,prod_name from products where prod_name like '% ton anvil'; +---------+--------------+ | prod_id | prod_name | +---------+--------------+ | ANV01 | .5 ton anvil | | ANV02 | 1 ton anvil | | ANV03 | 2 ton anvil | +---------+--------------+ 3 rows in set (0.00 sec)
通配符頗有用,但也是有代價的:通配符搜索的處理通常要比操做符搜索要花更長的時間。
通配符使用須要注意的幾點:
例1:檢索列prod_name包含文本1000的全部行
mysql> select prod_name from products where prod_name regexp '1000' order by prod_name;
+--------------+ | prod_name | +--------------+ | JetPack 1000 | +--------------+ 1 row in set (0.01 sec)
例2:
mysql> select prod_name from products where prod_name regexp '.000' order by prod_name;
+--------------+ | prod_name | +--------------+ | JetPack 1000 | | JetPack 2000 | +--------------+ 2 rows in set (0.00 sec)
正則語法:"."匹配任意一個字符
比較:LIKE
mysql> select prod_name from products where prod_name like '1000' order by prod_name; Empty set (0.00 sec) mysql> select prod_name from products where prod_name like '%1000' order by prod_name; +--------------+ | prod_name | +--------------+ | JetPack 1000 | +--------------+ 1 row in set (0.00 sec)
解釋:
LIKE匹配的是整個列,若是被匹配的文本在列值中出現,LIKE將不會找到它,相應的行也不被返回(除非使用通配符 -% _);
而REGEXP在列值內進行匹配,若是匹配的文本在列值中出現,REGEXP將會找到它,相應的行將被返回
匹配大小寫的問題:
MySQL在本本3.23.4後不區分大小寫,爲區分大小寫使用關鍵字:BINARTY
爲搜索兩個串之一使用---'|'
例:
mysql> select prod_name from products where prod_name REGEXP '1000 | 2000' order by prod_name; +--------------+ | prod_name | +--------------+ | JetPack 2000 | +--------------+ 1 row in set (0.00 sec) mysql> select prod_name from products where prod_name REGEXP '1000|2000' order by prod_name; +--------------+ | prod_name | +--------------+ | JetPack 1000 | | JetPack 2000 | +--------------+ 2 rows in set (0.00 sec)
注:注意書寫正則的格式,嚴格按照規範,不然匹配的徹底不一樣結果。
拓:
能夠給出多個條件:
select prod_name from products where prod_name REGEXP '1000|2000|.ton' order by prod_name; +--------------+ | prod_name | +--------------+ | .5 ton anvil | | 1 ton anvil | | 2 ton anvil | | Detonator | | JetPack 1000 | | JetPack 2000 | +--------------+ 6 rows in set (0.00 sec)
匹配特定的的字符,能夠指定一組用''[]"括起來的字符完成;
select prod_name from products where prod_name REGEXP '[123] Ton' order by prod_name;
+-------------+ | prod_name | +-------------+ | 1 ton anvil | | 2 ton anvil | +-------------+ 2 rows in set (0.00 sec)
[]是另外一種形式的OR語句,最好使用[],雖然功能與|相同,可是|有時候有歧義,
select prod_name from products where prod_name REGEXP '1|2|3 Ton' order by prod_name;
+---------------+ | prod_name | +---------------+ | 1 ton anvil | | 2 ton anvil | | JetPack 1000 | | JetPack 2000 | | TNT (1 stick) | +---------------+ 5 rows in set (0.00 sec)
正確的寫法須要加():
select prod_name from products where prod_name REGEXP '(1|2|3) Ton' order by prod_name; +-------------+ | prod_name | +-------------+ | 1 ton anvil | | 2 ton anvil | +-------------+ 2 rows in set (0.00 sec)
與匹配[123]相反的是匹配[123]以外的:[^123
]
select prod_name from products where prod_name REGEXP '[^123] Ton' order by prod_name; +--------------+ | prod_name | +--------------+ | .5 ton anvil | +--------------+ 1 row in set (0.00 sec)
拓:[123]--->[1-3] (定義範圍)
例:匹配包含 「 . 」字符的值
爲匹配特殊字符,必須用\\
做爲前導;\\-
表示查找-,\\.
表示查找 .;---->轉義
select vend_name from vendors where vend_name regexp '\\.' order by vend_name;
+--------------+ | vend_name | +--------------+ | Furball Inc. | +--------------+ 1 row in set (0.00 sec)
假設咱們須要匹配經常使用特殊字符便可這麼寫:
\\[ 匹配左方括號 \\. 匹配點號 \\] 匹配右方括號 \\| 匹配豎線 \\\ 匹配反斜槓本身自己
依次類推,其餘特殊的字符串也可使用這麼方式處理。
雙反斜槓加上一些字母還能夠表示特殊的含義。
好比:
\\f 換頁 \\n 換行 \\r 回車 \\t 製表符 \\v 縱向製表符
在通常的編程語言中,轉義通常使用一個反斜線,在Mysql中爲何是兩個才行?緣由是:Mysql本身須要一個來識別,而後Mysql會將扣除了一個反斜槓的剩餘的部分徹底的交給正則表達式庫解釋,因此加起來就是兩個了。
咱們直接給出表直接參閱。
類 | 說明 |
---|---|
[:alnum:] | 任意數字和字母。至關於[a-zA-Z0-9] |
[:alpha:] | 任意字符。至關於[a-zA-z] |
[:blank:] | 空格和製表。至關於[(雙斜槓,segmentfault這裏雙斜槓打不出來)t] |
[:cntrl:] | ASCII控制字符(ASCII 0 到31和127) |
[:digit:] | 任意數字。至關於[0-9] |
[:graph:] | 與[:print:]相同,可是不包含空格 |
[:lower:] | 任意的小寫字母。至關於[a-z] |
[:print:] | 任意可打印字符 |
[:punct:] | 既不在[:alnum:]又不在[:cntrl:]中的任意字符 |
[:space:] | 包括空格在內的任意空白字符。 |
[:upper:] | 任意大寫字母。至關於[A-Z] |
[:xdigit:] | 任意十六進制的數字。至關於[a-fA-F0-9] |
正則表達式重複元字符:
元字符 | 做用 |
---|---|
* | 重複0次或者屢次 |
+ | 重複一次或者屢次。至關於{1,} |
? | 重複0次或者1次 |
{n} | 重複n次 |
{n,} | 重複至少n次 |
{n,m} | 重複n-m次 |
例:
select prod_name from products where prod_name regexp '\\([0-9] sticks?\\)' order by prod_name;
+----------------+ | prod_name | +----------------+ | TNT (1 stick) | | TNT (5 sticks) | +----------------+ 2 rows in set (0.00 sec)
解釋:
\\( 匹配 \\)
【0-9】-->匹配範圍中的任意數字,sticks?匹配stick+sticks,(s後面的?使s可選)
例:
select prod_name from products where prod_name regexp '[[:digit:]]{4}' order by prod_name;
--------------+ | prod_name | +--------------+ | JetPack 1000 | | JetPack 2000 | +--------------+
解釋:
[[:digit:]]{4}匹配連在一塊兒的任意4位數字。
^ | 文本開始 |
---|---|
$ | 文本結束 |
[[:<:]] | 詞的開始 |
[[:>:]] | 詞的結束 |
^ 的雙重做用:(1)集合中,否認集合;(2)表示文本開始
例:找出一個數(包括以小數點開始的數)開始的全部產品;
select prod_name from products prod_name regexp '^[0-9\\.]' order by prod_name;
+--------------+ | prod_name | +--------------+ | .5 ton anvil | | 1 ton anvil | | 2 ton anvil | +--------------+ 3 rows in set (0.00 sec)
對照沒有^:
select prod_name from products where prod_name regexp '[0-9\\.]' order by prod_name; +----------------+ | prod_name | +----------------+ | .5 ton anvil | | 1 ton anvil | | 2 ton anvil | | JetPack 1000 | | JetPack 2000 | | TNT (1 stick) | | TNT (5 sticks) | +----------------+ 7 rows in set (0.00 sec)
解釋:^匹配串的開始,所以[0-9\.]只在.或者任意數字爲串中第一個字符時菜匹配他們。
^的雙重用途:
在集合中[^]用來否認該集合,其餘則用來指串的開始處。
計算字段與列不一樣,計算字段並不實際存在與數據庫表中,計算字段是運行時在select語句中建立的;
字段:基本與列的意思相同,常常互換使用,不過數據庫列通常稱爲列。
拼接(concatenate)將值聯結到一塊兒構成單個值;
函數:Concat()
注:多數DBMS使用+或者||拼接
mysql> select concat(vend_name,"(",vend_country,")") from vendors order by vend_name ; +----------------------------------------+ | concat(vend_name,"(",vend_country,")") | +----------------------------------------+ | ACME(USA) | | Anvils R Us(USA) | | Furball Inc.(USA) | | Jet Set(England) | | Jouets Et Ours(France) | | LT Supplies(USA) | +----------------------------------------+ 6 rows in set (0.01 sec)
Concat()拼接串,須要一個或多個指定的串,各個串之間用逗號分割;
RTrim()函數:刪除數據右側多餘的空格來整理數據;
select concat(rtrim(vend_name),"(",rtrim(vend_country),")") from vendors order by vend_name ; +------------------------------------------------------+ | concat(rtrim(vend_name),"(",rtrim(vend_country),")") | +------------------------------------------------------+ | ACME(USA) | | Anvils R Us(USA) | | Furball Inc.(USA) | | Jet Set(England) | | Jouets Et Ours(France) | | LT Supplies(USA) | +------------------------------------------------------+ 6 rows in set (0.02 sec)
LTrim()去掉串左邊的空格,Trim()除去串左右兩邊的空格
關鍵字:AS
mysql> select concat(rtrim(vend_name),"(",rtrim(vend_country),")") as vend_title from vendors order by vend_name ; +------------------------+ | vend_title | +------------------------+ | ACME(USA) | | Anvils R Us(USA) | | Furball Inc.(USA) | | Jet Set(England) | | Jouets Et Ours(France) | | LT Supplies(USA) | +------------------------+ 6 rows in set (0.00 sec)
AS指示SQL建立一個包含指定計算的名爲vend_title 的計算字段,與實際的表同樣能夠調用;
別名有時也稱導出列,表明的內容相同。
首先檢索訂單號20005中的全部物品:
mysql> select * from orderitems where order_num =20005; +-----------+------------+---------+----------+------------+ | order_num | order_item | prod_id | quantity | item_price | +-----------+------------+---------+----------+------------+ | 20005 | 1 | ANV01 | 10 | 5.99 | | 20005 | 2 | ANV02 | 3 | 9.99 | | 20005 | 3 | TNT2 | 5 | 10.00 | | 20005 | 4 | FB | 1 | 10.00 | +-----------+------------+---------+----------+------------+ 4 rows in set (0.01 sec)
彙總物品的價格:單價乘以數量
mysql> select prod_id,quantity,item_price,quantity*item_price AS expanded_price from orderitems where order_num = 20005; +---------+----------+------------+----------------+ | prod_id | quantity | item_price | expanded_price | +---------+----------+------------+----------------+ | ANV01 | 10 | 5.99 | 59.90 | | ANV02 | 3 | 9.99 | 29.97 | | TNT2 | 5 | 10.00 | 50.00 | | FB | 1 | 10.00 | 10.00 | +---------+----------+------------+----------------+
關鍵字:upper()
做用:將文本轉換成大寫
select vend_name,Upper(vend_name) as vend_name_upase from vendors order by vend_name; +----------------+-----------------+ | vend_name | vend_name_upase | +----------------+-----------------+ | ACME | ACME | | Anvils R Us | ANVILS R US | | Furball Inc. | FURBALL INC. | | Jet Set | JET SET | | Jouets Et Ours | JOUETS ET OURS | | LT Supplies | LT SUPPLIES | +----------------+-----------------+ 6 rows in set (0.00 sec)
經常使用的文本處理函數:(詳細解釋待補充)
函數 | 說明 |
---|---|
left() | 返回串左邊的字符 |
length() | 返回串的長度 |
locate() | 找出串的一個字串 |
lower() | 將串轉換成小寫 |
LTrim() | 去掉左邊的空格 |
Right() | 返回串右邊的字符 |
RTrim() | 去掉串右邊的空格 |
Soundex() | 返回串的SOUNDEX的值 |
SubString() | 返回字串的字符 |
Upper() | 將串轉換成大寫 |
其中SOUNDEX解釋:
我的理解模糊匹配相同的發音;
mysql> select cust_name , cust_contact from customers; +----------------+--------------+ | cust_name | cust_contact | +----------------+--------------+ | Coyote Inc. | Y Lee | | Mouse House | Jerry Mouse | | Wascals | Jim Jones | | Yosemite Place | Y Sam | | E Fudd | E Fudd | +----------------+--------------+ 5 rows in set (0.00 sec)
假如咱們想查找Y Lee聯繫人,但實際咱們查找輸入的使L Lie,那麼沒法獲得所須要的;
mysql> select cust_name,cust_contact from customers where cust_contact ="Y.Lie"; Empty set (0.00 sec)
咱們可使用SOUNDEX進行音節的模糊匹配:
mysql> select cust_name,cust_contact from customers where soundex(cust_contact) =soundex("Y.Lie"); +-------------+--------------+ | cust_name | cust_contact | +-------------+--------------+ | Coyote Inc. | Y Lee | +-------------+--------------+ 1 row in set (0.00 sec)
使用形式:大多數被用來讀取、統計和處理這些值
經常使用日期和時間處理函數:(待補充)
使用日期格式的注意點:
無論插入仍是更新仍是用where進行過濾,日期格式必須yyy-mm-dd;
mysql> select cust_id,order_num from orders where order_date ="2005-09-01"; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | +---------+-----------+ 1 row in set (0.00 sec) mysql> select * from orders; +-----------+---------------------+---------+ | order_num | order_date | cust_id | +-----------+---------------------+---------+ | 20005 | 2005-09-01 00:00:00 | 10001 | | 20006 | 2005-09-12 00:00:00 | 10003 | | 20007 | 2005-09-30 00:00:00 | 10004 | | 20008 | 2005-10-03 00:00:00 | 10005 | | 20009 | 2005-10-08 00:00:00 | 10001 | +-----------+---------------------+---------+ 5 rows in set (0.00 sec)
關鍵字:date()
做用:僅提取列的日期部分
mysql> select cust_id,order_num from orders where date(order_date) ="2005-09-01"; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | +---------+-----------+ 1 row in set (0.00 sec)
關鍵字:between and
例:檢索2005年9月下的全部訂單
mysql> select cust_id ,order_num from orders where date(order_date ) between "2005-09-01" and "2005-09-30"; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | | 10003 | 20006 | | 10004 | 20007 | +---------+-----------+ 3 rows in set (0.00 sec)
解2:
mysql> select cust_id ,order_num from orders where Year(order_date ) = 2005 and Month(order_date )=9; +---------+-----------+ | cust_id | order_num | +---------+-----------+ | 10001 | 20005 | | 10003 | 20006 | | 10004 | 20007 | +---------+-----------+ 3 rows in set (0.00 sec)
定義:數值處理僅處理數值數據,通常用於代數或者幾個運算;(詳細解釋待補充)
定義:彙集函數運行在行組上,計算和返回單個值的函數;
函數 | 說明 |
---|---|
AVG() | 返回某列的平均值 |
COUNT() | 返回某列的行數 |
MAX() | 返回某列的最大值 |
MIN() | 返回某列的最小值 |
SUN() | 返回某列值之和 |
做用:返回全部列的平均值,也可返回特定列的平均值
mysql> select avg(prod_price) as avg_price from products; +-----------+ | avg_price | +-----------+ | 16.133571 | +-----------+ 1 row in set (0.06 sec)
mysql> select prod_price from products; +------------+ | prod_price | +------------+ | 5.99 | | 9.99 | | 14.99 | | 13.00 | | 10.00 | | 2.50 | | 3.42 | | 35.00 | | 55.00 | | 8.99 | | 50.00 | | 4.49 | | 2.50 | | 10.00 | +------------+ 14 rows in set (0.00 sec)
注:
爲得到多個列的平均值,必須使用多個AVG()函數;
NULL值,AVG()函數忽略列值爲NULL的行;
兩種使用方法:
#返回客戶數量--行 mysql> select count(*) from customers; +----------+ | count(*) | +----------+ | 5 | +----------+ 1 row in set (0.00 sec)
統計具備電子郵件地址的客戶數量:
初始狀態:
mysql> select cust_email from customers; +---------------------+ | cust_email | +---------------------+ | ylee@coyote.com | | NULL | | rabbit@wascally.com | | sam@yosemite.com | | NULL | +---------------------+ 5 rows in set (0.00 sec)
篩選:
mysql> select count(cust_email )from customers; +--------------------+ | count(cust_email ) | +--------------------+ | 3 | +--------------------+ 1 row in set (0.00 sec)
特色:都須要指定列名;
mysql> select max(prod_price ) as max_price from products; +-----------+ | max_price | +-----------+ | 55.00 | +-----------+ 1 row in set (0.00 sec) mysql> select min(prod_price ) as min_price from products; +-----------+ | min_price | +-----------+ | 2.50 | +-----------+ 1 row in set (0.00 sec)
做用:指定列值的和
類似:count計算行值的個數
mysql> select sum(quantity ) as items_ordered from orderitems where order_num = 20005; +---------------+ | items_ordered | +---------------+ | 19 | +---------------+ 1 row in set (0.00 sec)
mysql> select quantity,order_num from orderitems; +----------+-----------+ | quantity | order_num | +----------+-----------+ | 10 | 20005 | | 3 | 20005 | | 5 | 20005 | | 1 | 20005 | | 1 | 20006 | | 100 | 20007 | | 50 | 20008 | | 1 | 20009 | | 1 | 20009 | | 1 | 20009 | | 1 | 20009 | +----------+-----------+ 11 rows in set (0.00 sec)
注:
sum()函數忽略列值爲NULl的行;
計算不一樣的值須要指定DISTINCT參數;
mysql> select avg(distinct prod_price ) as avg_price from products where vend_id =1003; +-----------+ | avg_price | +-----------+ | 15.998000 | +-----------+ 1 row in set (0.00 sec)
做用的函數:count(),可是不能用於count(*)
distinct()必須用於列名,不能用於計算或者表達式;
mysql> select count(*) as num_items,min(prod_price ) as price_min,max(prod_price ) as price_max -> ,avg(prod_price ) as price_avg from products; +-----------+-----------+-----------+-----------+ | num_items | price_min | price_max | price_avg | +-----------+-----------+-----------+-----------+ | 14 | 2.50 | 55.00 | 16.133571 | +-----------+-----------+-----------+-----------+ 1 row in set (0.00 sec)