6.1、基本查詢語(yǔ)句
創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元德州做網(wǎng)站,已為上家服務(wù),為德州各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
MySQL從數(shù)據(jù)表中查詢數(shù)據(jù)的基本語(yǔ)句為SELECT語(yǔ)句,其基本格式為:
SELECT {* | <字段列表>} [ FROM <表1>,<表2>... [WHERE 表達(dá)式 [GROUP BY <group by definition>] [HAVING <expression> [{<operator> <expression>}...]] [ORDER BY <order by definition>] [LIMIT [<offset>,] <row count> ] ] ] SELECT [字段1,字段2,...,字段n] FROM [表或視圖] WHERE [查詢條件]
各條子句的含義:
{* | <字段列表>}:包含星號(hào)通配符選擇字段列表,表示查詢的字段,其字段列至少包含一個(gè)字段名稱。 FROM <表1>,<表2>...:表1,表2表示查詢數(shù)據(jù)的來(lái)源 WHERE 表達(dá)式:可選項(xiàng),限定查詢必須滿足的查詢條件 GROUP BY <字段>:告訴MySQL如何顯示查詢出來(lái)的數(shù)據(jù),并按照指定字段分組 ORDER BY <字段>:告訴MySQL按什么順序顯示查詢的數(shù)據(jù),可進(jìn)行的排序ASC,DESC LIMIT [<offset>,] <row count>:告訴MySQL每次顯示查詢出來(lái)的數(shù)據(jù)條數(shù) |
mysql> CREATE TABLE fruits -> ( -> f_id char(10) NOT NULL, -> s_id INT NOT NULL, -> f_name char(255) NOT NULL, -> f_price decimal(8,2) NOT NULL, -> PRIMARY KEY(f_id) -> ); Query OK, 0 rows affected (0.05 sec) mysql> INSERT INTO fruits (f_id, s_id, f_name, f_price) -> VALUES('a1', 101,'apple',5.2), -> ('b1',101,'blackberry', 10.2), -> ('bs1',102,'orange', 11.2), -> ('bs2',105,'melon',8.2), -> ('t1',102,'banana', 10.3), -> ('t2',102,'grape', 5.3), -> ('o2',103,'coconut', 9.2), -> ('c0',101,'cherry', 3.2), -> ('a2',103, 'apricot',2.2), -> ('l2',104,'lemon', 6.4), -> ('b2',104,'berry', 7.6), -> ('m1',106,'mango', 15.6), -> ('m2',105,'xbabay', 2.6), -> ('t4',107,'xbababa', 3.6), -> ('m3',105,'xxtt', 11.6), -> ('b5',107,'xxxx', 3.6); Query OK, 16 rows affected (0.09 sec) Records: 16 Duplicates: 0 Warnings: 0
6.2、單表查詢
查詢所有字段
語(yǔ)法: SELECT * FROM 表名
mysql> SELECT * FROM fruits; +------+------+------------+---------+ | f_id | s_id | f_name | f_price | +------+------+------------+---------+ | a1 | 101 | apple | 5.20 | | a2 | 103 | apricot | 2.20 | | b1 | 101 | blackberry | 10.20 | | b2 | 104 | berry | 7.60 | | b5 | 107 | xxxx | 3.60 | | bs1 | 102 | orange | 11.20 | | bs2 | 105 | melon | 8.20 | | c0 | 101 | cherry | 3.20 | | l2 | 104 | lemon | 6.40 | | m1 | 106 | mango | 15.60 | | m2 | 105 | xbabay | 2.60 | | m3 | 105 | xxtt | 11.60 | | o2 | 103 | coconut | 9.20 | | t1 | 102 | banana | 10.30 | | t2 | 102 | grape | 5.30 | | t4 | 107 | xbababa | 3.60 | +------+------+------------+---------+ 16 rows in set (0.00 sec)
查詢指定字段
語(yǔ)法: SELECT 字段名1,字段名2,... FROM 表名
mysql> SELECT f_name FROM fruits; +------------+ | f_name | +------------+ | apple | | apricot | | blackberry | | berry | | xxxx | | orange | | melon | | cherry | | lemon | | mango | | xbabay | | xxtt | | coconut | | banana | | grape | | xbababa | +------------+ 16 rows in set (0.00 sec) mysql> SELECT f_name,f_price FROM fruits; +------------+---------+ | f_name | f_price | +------------+---------+ | apple | 5.20 | | apricot | 2.20 | | blackberry | 10.20 | | berry | 7.60 | | xxxx | 3.60 | | orange | 11.20 | | melon | 8.20 | | cherry | 3.20 | | lemon | 6.40 | | mango | 15.60 | | xbabay | 2.60 | | xxtt | 11.60 | | coconut | 9.20 | | banana | 10.30 | | grape | 5.30 | | xbababa | 3.60 | +------------+---------+ 16 rows in set (0.00 sec)
查詢指定記錄
語(yǔ)法: SELECT 字段名1,字段名2,... FROM 表名 WHERE 查詢條件
WHERE條件判斷符
操作符 | 說(shuō)明 |
= | 相等 |
<>,!= | 不等于 |
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
BETWEEN | 位于兩值之間 |
mysql> SELECT f_name, f_price -> FROM fruits -> WHERE f_price = 10.2; +------------+---------+ | f_name | f_price | +------------+---------+ | blackberry | 10.20 | +------------+---------+ 1 row in set (0.00 sec) mysql> SELECT f_name, f_price -> FROM fruits -> WHERE f_name = 'apple'; +--------+---------+ | f_name | f_price | +--------+---------+ | apple | 5.20 | +--------+---------+ 1 row in set (0.00 sec) mysql> SELECT f_name, f_price -> FROM fruits -> WHERE f_price < 10; +---------+---------+ | f_name | f_price | +---------+---------+ | apple | 5.20 | | apricot | 2.20 | | berry | 7.60 | | xxxx | 3.60 | | melon | 8.20 | | cherry | 3.20 | | lemon | 6.40 | | xbabay | 2.60 | | coconut | 9.20 | | grape | 5.30 | | xbababa | 3.60 | +---------+---------+ 11 rows in set (0.00 sec)
帶IN關(guān)鍵字的查詢
IN操作符用來(lái)查詢滿足指定范圍內(nèi)的條件記錄,使用IN操作符,將所有檢索條件用括號(hào)括起來(lái),檢索條件之間用逗號(hào)分隔開(kāi),只要滿足條件范圍內(nèi)的一個(gè)值即可。
mysql> SELECT s_id,f_name, f_price -> FROM fruits -> WHERE s_id IN (101,102) -> ORDER BY f_name; +------+------------+---------+ | s_id | f_name | f_price | +------+------------+---------+ | 101 | apple | 5.20 | | 102 | banana | 10.30 | | 101 | blackberry | 10.20 | | 101 | cherry | 3.20 | | 102 | grape | 5.30 | | 102 | orange | 11.20 | +------+------------+---------+ 6 rows in set (0.00 sec) mysql> SELECT s_id,f_name, f_price -> FROM fruits -> WHERE s_id NOT IN (101,102) -> ORDER BY f_name; +------+---------+---------+ | s_id | f_name | f_price | +------+---------+---------+ | 103 | apricot | 2.20 | | 104 | berry | 7.60 | | 103 | coconut | 9.20 | | 104 | lemon | 6.40 | | 106 | mango | 15.60 | | 105 | melon | 8.20 | | 107 | xbababa | 3.60 | | 105 | xbabay | 2.60 | | 105 | xxtt | 11.60 | | 107 | xxxx | 3.60 | +------+---------+---------+ 10 rows in set (0.00 sec)
帶BETWEEN AND的范圍查詢
BETWEEN AND用來(lái)查詢某個(gè)范圍內(nèi)的值,該操作符需要兩個(gè)參數(shù),即范圍的開(kāi)始和結(jié)束,如果字段滿足指定的范圍查詢條件,則這些記錄被返回。
mysql> SELECT f_name, f_price -> FROM fruits -> WHERE f_price BETWEEN 2.00 AND 10.20; +------------+---------+ | f_name | f_price | +------------+---------+ | apple | 5.20 | | apricot | 2.20 | | blackberry | 10.20 | | berry | 7.60 | | xxxx | 3.60 | | melon | 8.20 | | cherry | 3.20 | | lemon | 6.40 | | xbabay | 2.60 | | coconut | 9.20 | | grape | 5.30 | | xbababa | 3.60 | +------------+---------+ 12 rows in set (0.00 sec) mysql> SELECT f_name, f_price -> FROM fruits -> WHERE f_price NOT BETWEEN 2.00 AND 10.20; +--------+---------+ | f_name | f_price | +--------+---------+ | orange | 11.20 | | mango | 15.60 | | xxtt | 11.60 | | banana | 10.30 | +--------+---------+ 4 rows in set (0.00 sec)
帶LIKE的字符匹配查詢
%通配符,匹配任意長(zhǎng)度任意字符
mysql> SELECT f_id, f_name -> FROM fruits -> WHERE f_name LIKE 'b%'; +------+------------+ | f_id | f_name | +------+------------+ | b1 | blackberry | | b2 | berry | | t1 | banana | +------+------------+ 3 rows in set (0.00 sec) mysql> SELECT f_id, f_name -> FROM fruits -> WHERE f_name LIKE '%g%'; +------+--------+ | f_id | f_name | +------+--------+ | bs1 | orange | | m1 | mango | | t2 | grape | +------+--------+ 3 rows in set (0.00 sec)
_通配符:匹配單個(gè)字符
mysql> SELECT f_id, f_name FROM fruits WHERE f_name LIKE '____e'; +------+--------+ | f_id | f_name | +------+--------+ | a1 | apple | | t2 | grape | +------+--------+ 2 rows in set (0.00 sec)
查詢空值
數(shù)據(jù)表創(chuàng)建的時(shí)候,可以指定某列中可以包含空值(NULL),空值不同于0,也不同于空字符串??罩狄话惚硎緮?shù)據(jù)未知、不適用或?qū)⒃谝院筇砑訑?shù)據(jù)。使用IS NULL子句可以查詢某字段內(nèi)容為空的記錄。
mysql> CREATE TABLE customers -> ( -> c_id int NOT NULL AUTO_INCREMENT, -> c_name char(50) NOT NULL, -> c_address char(50) NULL, -> c_city char(50) NULL, -> c_zip char(10) NULL, -> c_contact char(50) NULL, -> c_email char(255) NULL, -> PRIMARY KEY (c_id) -> ); Query OK, 0 rows affected (0.02 sec) mysql> INSERT INTO customers(c_id, c_name, c_address, c_city, -> c_zip, c_contact, c_email) -> VALUES(10001, 'RedHook', '200 Street ', 'Tianjin', -> '300000', 'LiMing', 'LMing@163.com'), -> (10002, 'Stars', '333 Fromage Lane', -> 'Dalian', '116000', 'Zhangbo','Jerry@hotmail.com'), -> (10003, 'Netbhood', '1 Sunny Place', 'Qingdao', '266000', -> 'LuoCong', NULL), -> (10004, 'JOTO', '829 Riverside Drive', 'Haikou', -> '570000', 'YangShan', 'sam@hotmail.com'); Query OK, 4 rows affected (0.02 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NULL; +-------+----------+---------+ | c_id | c_name | c_email | +-------+----------+---------+ | 10003 | Netbhood | NULL | +-------+----------+---------+ 1 row in set (0.00 sec) mysql> SELECT c_id, c_name,c_email FROM customers WHERE c_email IS NOT NULL; +-------+---------+-------------------+ | c_id | c_name | c_email | +-------+---------+-------------------+ | 10001 | RedHook | LMing@163.com | | 10002 | Stars | Jerry@hotmail.com | | 10004 | JOTO | sam@hotmail.com | +-------+---------+-------------------+ 3 rows in set (0.00 sec)
帶AND的多條件查詢
mysql> SELECT f_id, f_price, f_name FROM fruits WHERE s_id = '101' AND f_price >=5; +------+---------+------------+ | f_id | f_price | f_name | +------+---------+------------+ | a1 | 5.20 | apple | | b1 | 10.20 | blackberry | +------+---------+------------+ 2 rows in set (0.00 sec) mysql> SELECT f_id, f_price, f_name FROM fruits -> WHERE s_id IN('101', '102') AND f_price >= 5 AND f_name = 'apple'; +------+---------+--------+ | f_id | f_price | f_name | +------+---------+--------+ | a1 | 5.20 | apple | +------+---------+--------+ 1 row in set (0.00 sec)
帶OR的多條件查詢
mysql> SELECT s_id,f_name, f_price -> FROM fruits -> WHERE s_id = 101 OR s_id = 102; +------+------------+---------+ | s_id | f_name | f_price | +------+------------+---------+ | 101 | apple | 5.20 | | 101 | blackberry | 10.20 | | 102 | orange | 11.20 | | 101 | cherry | 3.20 | | 102 | banana | 10.30 | | 102 | grape | 5.30 | +------+------------+---------+ 6 rows in set (0.00 sec) mysql> SELECT s_id,f_name, f_price -> FROM fruits -> WHERE s_id IN(101,102); +------+------------+---------+ | s_id | f_name | f_price | +------+------------+---------+ | 101 | apple | 5.20 | | 101 | blackberry | 10.20 | | 102 | orange | 11.20 | | 101 | cherry | 3.20 | | 102 | banana | 10.30 | | 102 | grape | 5.30 | +------+------------+---------+ 6 rows in set (0.00 sec)
OR操作符和IN操作符使用后結(jié)果一樣,可以實(shí)現(xiàn)相同的功能。但是使用IN操作符使檢索語(yǔ)句更加簡(jiǎn)明,并且IN執(zhí)行的速度更快于OR。
查詢結(jié)果不重復(fù)
MySQL中使用DISTINCT關(guān)鍵字消除重復(fù)的記錄,其語(yǔ)法為:
SELECT DISTINCT 字段名 FROM 表名
mysql> SELECT s_id FROM fruits; +------+ | s_id | +------+ | 101 | | 103 | | 101 | | 104 | | 107 | | 102 | | 105 | | 101 | | 104 | | 106 | | 105 | | 105 | | 103 | | 102 | | 102 | | 107 | +------+ 16 rows in set (0.00 sec) mysql> SELECT DISTINCT s_id FROM fruits; +------+ | s_id | +------+ | 101 | | 103 | | 104 | | 107 | | 102 | | 105 | | 106 | +------+ 7 rows in set (0.00 sec)
對(duì)查詢的結(jié)果排序
單列排序
mysql> SELECT f_name FROM fruits; +------------+ | f_name | +------------+ | apple | | apricot | | blackberry | | berry | | xxxx | | orange | | melon | | cherry | | lemon | | mango | | xbabay | | xxtt | | coconut | | banana | | grape | | xbababa | +------------+ 16 rows in set (0.00 sec) mysql> SELECT f_name FROM fruits ORDER BY f_name; +------------+ | f_name | +------------+ | apple | | apricot | | banana | | berry | | blackberry | | cherry | | coconut | | grape | | lemon | | mango | | melon | | orange | | xbababa | | xbabay | | xxtt | | xxxx | +------------+ 16 rows in set (0.00 sec)
多列排序
mysql> SELECT f_name, f_price FROM fruits ORDER BY f_name, f_price; +------------+---------+ | f_name | f_price | +------------+---------+ | apple | 5.20 | | apricot | 2.20 | | banana | 10.30 | | berry | 7.60 | | blackberry | 10.20 | | cherry | 3.20 | | coconut | 9.20 | | grape | 5.30 | | lemon | 6.40 | | mango | 15.60 | | melon | 8.20 | | orange | 11.20 | | xbababa | 3.60 | | xbabay | 2.60 | | xxtt | 11.60 | | xxxx | 3.60 | +------------+---------+ 16 rows in set (0.00 sec)
指定方向排序
mysql> SELECT f_name, f_price FROM fruits ORDER BY f_price DESC; +------------+---------+ | f_name | f_price | +------------+---------+ | mango | 15.60 | | xxtt | 11.60 | | orange | 11.20 | | banana | 10.30 | | blackberry | 10.20 | | coconut | 9.20 | | melon | 8.20 | | berry | 7.60 | | lemon | 6.40 | | grape | 5.30 | | apple | 5.20 | | xxxx | 3.60 | | xbababa | 3.60 | | cherry | 3.20 | | xbabay | 2.60 | | apricot | 2.20 | +------------+---------+ 16 rows in set (0.00 sec) mysql> SELECT f_price, f_name FROM fruits ORDER BY f_price DESC, f_name; +---------+------------+ | f_price | f_name | +---------+------------+ | 15.60 | mango | | 11.60 | xxtt | | 11.20 | orange | | 10.30 | banana | | 10.20 | blackberry | | 9.20 | coconut | | 8.20 | melon | | 7.60 | berry | | 6.40 | lemon | | 5.30 | grape | | 5.20 | apple | | 3.60 | xbababa | | 3.60 | xxxx | | 3.20 | cherry | | 2.60 | xbabay | | 2.20 | apricot | +---------+------------+ 16 rows in set (0.00 sec)
分組查詢
分組查詢是對(duì)數(shù)據(jù)按照某個(gè)或多個(gè)字段進(jìn)行分組,MySQL中使用GROUP BY 關(guān)鍵字對(duì)數(shù)據(jù)進(jìn)行分組,基本語(yǔ)法形式為:
[GROUP BY 字段] [HAVING <條件表達(dá)式>]
創(chuàng)建分組
GROUP BY關(guān)鍵字通常和集合函數(shù)一起使用,如MAX()、MIN()、COUNT()、SUM()、AVG()。
mysql> SELECT s_id, COUNT(*) AS Total FROM fruits GROUP BY s_id; +------+-------+ | s_id | Total | +------+-------+ | 101 | 3 | | 102 | 3 | | 103 | 2 | | 104 | 2 | | 105 | 3 | | 106 | 1 | | 107 | 2 | +------+-------+ 7 rows in set (0.00 sec) mysql> SELECT s_id, GROUP_CONCAT(f_name) AS Names FROM fruits GROUP BY s_id; +------+-------------------------+ | s_id | Names | +------+-------------------------+ | 101 | apple,blackberry,cherry | | 102 | grape,banana,orange | | 103 | apricot,coconut | | 104 | lemon,berry | | 105 | xbabay,xxtt,melon | | 106 | mango | | 107 | xxxx,xbababa | +------+-------------------------+ 7 rows in set (0.00 sec)
使用HAVING過(guò)濾分組
mysql> SELECT s_id, GROUP_CONCAT(f_name) AS Names -> FROM fruits -> GROUP BY s_id HAVING COUNT(f_name) > 1; +------+-------------------------+ | s_id | Names | +------+-------------------------+ | 101 | apple,blackberry,cherry | | 102 | grape,banana,orange | | 103 | apricot,coconut | | 104 | lemon,berry | | 105 | xbabay,xxtt,melon | | 107 | xxxx,xbababa | +------+-------------------------+ 6 rows in set (0.00 sec)
在GROUP BY 子句中使用WITH ROLLUP,顯示查詢出的所有記錄總和
mysql> SELECT s_id, COUNT(*) AS Total -> FROM fruits -> GROUP BY s_id WITH ROLLUP; +------+-------+ | s_id | Total | +------+-------+ | 101 | 3 | | 102 | 3 | | 103 | 2 | | 104 | 2 | | 105 | 3 | | 106 | 1 | | 107 | 2 | | NULL | 16 | +------+-------+ 8 rows in set (0.00 sec)
多字段分組
mysql> SELECT * FROM fruits group by s_id,f_name; +------+------+------------+---------+ | f_id | s_id | f_name | f_price | +------+------+------------+---------+ | a1 | 101 | apple | 5.20 | | b1 | 101 | blackberry | 10.20 | | c0 | 101 | cherry | 3.20 | | t1 | 102 | banana | 10.30 | | t2 | 102 | grape | 5.30 | | bs1 | 102 | orange | 11.20 | | a2 | 103 | apricot | 2.20 | | o2 | 103 | coconut | 9.20 | | b2 | 104 | berry | 7.60 | | l2 | 104 | lemon | 6.40 | | bs2 | 105 | melon | 8.20 | | m2 | 105 | xbabay | 2.60 | | m3 | 105 | xxtt | 11.60 | | m1 | 106 | mango | 15.60 | | t4 | 107 | xbababa | 3.60 | | b5 | 107 | xxxx | 3.60 | +------+------+------------+---------+ 16 rows in set (0.00 sec)
GROUP BY 和ORDER BY一起使用
mysql> CREATE TABLE orderitems -> ( -> o_num int NOT NULL, -> o_item int NOT NULL, -> f_id char(10) NOT NULL, -> quantity int NOT NULL, -> item_price decimal(8,2) NOT NULL, -> PRIMARY KEY (o_num,o_item) -> ) ; Query OK, 0 rows affected (0.09 sec) mysql> INSERT INTO orderitems(o_num, o_item, f_id, quantity, item_price) -> VALUES(30001, 1, 'a1', 10, 5.2), -> (30001, 2, 'b2', 3, 7.6), -> (30001, 3, 'bs1', 5, 11.2), -> (30001, 4, 'bs2', 15, 9.2), -> (30002, 1, 'b3', 2, 20.0), -> (30003, 1, 'c0', 100, 10), -> (30004, 1, 'o2', 50, 2.50), -> (30005, 1, 'c0', 5, 10), -> (30005, 2, 'b1', 10, 8.99), -> (30005, 3, 'a2', 10, 2.2), -> (30005, 4, 'm1', 5, 14.99); Query OK, 11 rows affected (0.08 sec) Records: 11 Duplicates: 0 Warnings: 0 mysql> SELECT o_num, SUM(quantity * item_price) AS orderTotal -> FROM orderitems -> GROUP BY o_num -> HAVING SUM(quantity*item_price) >= 100; +-------+------------+ | o_num | orderTotal | +-------+------------+ | 30001 | 268.80 | | 30003 | 1000.00 | | 30004 | 125.00 | | 30005 | 236.85 | +-------+------------+ 4 rows in set (0.00 sec) mysql> SELECT o_num, SUM(quantity * item_price) AS orderTotal -> FROM orderitems -> GROUP BY o_num -> HAVING SUM(quantity*item_price) >= 100 -> ORDER BY orderTotal; +-------+------------+ | o_num | orderTotal | +-------+------------+ | 30004 | 125.00 | | 30005 | 236.85 | | 30001 | 268.80 | | 30003 | 1000.00 | +-------+------------+ 4 rows in set (0.00 sec)
使用LIMIT限制查詢結(jié)果數(shù)量
使用LIMIT關(guān)鍵字,可以限制查詢結(jié)果的數(shù)量,語(yǔ)法格式為:
LIMIT [位置偏移量,] 行數(shù)
mysql> SELECT * From fruits LIMIT 4; +------+------+------------+---------+ | f_id | s_id | f_name | f_price | +------+------+------------+---------+ | a1 | 101 | apple | 5.20 | | a2 | 103 | apricot | 2.20 | | b1 | 101 | blackberry | 10.20 | | b2 | 104 | berry | 7.60 | +------+------+------------+---------+ 4 rows in set (0.00 sec) mysql> SELECT * From fruits LIMIT 4, 3; +------+------+--------+---------+ | f_id | s_id | f_name | f_price | +------+------+--------+---------+ | b5 | 107 | xxxx | 3.60 | | bs1 | 102 | orange | 11.20 | | bs2 | 105 | melon | 8.20 | +------+------+--------+---------+ 3 rows in set (0.00 sec)
6.3、使用聚合函數(shù)查詢
有時(shí)不需要返回實(shí)際表中的數(shù)據(jù),而只是針對(duì)數(shù)據(jù)進(jìn)行總結(jié)。MySQL提供一些查詢功能,可以獲取數(shù)據(jù)進(jìn)行分析和報(bào)告。
函數(shù) | 作用 |
AVG() | 返回某列的平均值 |
COUNT() | 返回某列的行數(shù) |
MAX() | 返回某列的最大值 |
MIN() | 返回某列的最小值 |
SUM() | 返回某列值的和 |
COUNT()函數(shù)
mysql> SELECT COUNT(*) AS cust_num -> FROM customers; +----------+ | cust_num | +----------+ | 4 | +----------+ 1 row in set (0.00 sec) mysql> SELECT COUNT(c_email) AS email_num -> FROM customers; +-----------+ | email_num | +-----------+ | 3 | +-----------+ 1 row in set (0.00 sec) mysql> SELECT o_num, COUNT(f_id) -> FROM orderitems -> GROUP BY o_num; +-------+-------------+ | o_num | COUNT(f_id) | +-------+-------------+ | 30001 | 4 | | 30002 | 1 | | 30003 | 1 | | 30004 | 1 | | 30005 | 4 | +-------+-------------+ 5 rows in set (0.00 sec)
SUM()函數(shù)
mysql> SELECT SUM(quantity) AS items_total -> FROM orderitems -> WHERE o_num = 30005; +-------------+ | items_total | +-------------+ | 30 | +-------------+ 1 row in set (0.00 sec) mysql> SELECT o_num, SUM(quantity) AS items_total -> FROM orderitems -> GROUP BY o_num; +-------+-------------+ | o_num | items_total | +-------+-------------+ | 30001 | 33 | | 30002 | 2 | | 30003 | 100 | | 30004 | 50 | | 30005 | 30 | +-------+-------------+ 5 rows in set (0.00 sec)
AVG()函數(shù)
mysql> SELECT AVG(f_price) AS avg_price -> FROM fruits -> WHERE s_id = 103; +-----------+ | avg_price | +-----------+ | 5.700000 | +-----------+ 1 row in set (0.00 sec) mysql> SELECT s_id,AVG(f_price) AS avg_price -> FROM fruits -> GROUP BY s_id; +------+-----------+ | s_id | avg_price | +------+-----------+ | 101 | 6.200000 | | 102 | 8.933333 | | 103 | 5.700000 | | 104 | 7.000000 | | 105 | 7.466667 | | 106 | 15.600000 | | 107 | 3.600000 | +------+-----------+ 7 rows in set (0.00 sec)
MAX()函數(shù)
mysql> SELECT MAX(f_price) AS max_price FROM fruits; +-----------+ | max_price | +-----------+ | 15.60 | +-----------+ 1 row in set (0.00 sec) mysql> SELECT s_id, MAX(f_price) AS max_price -> FROM fruits -> GROUP BY s_id; +------+-----------+ | s_id | max_price | +------+-----------+ | 101 | 10.20 | | 102 | 11.20 | | 103 | 9.20 | | 104 | 7.60 | | 105 | 11.60 | | 106 | 15.60 | | 107 | 3.60 | +------+-----------+ 7 rows in set (0.00 sec) mysql> SELECT MAX(f_name) FROM fruits; +-------------+ | MAX(f_name) | +-------------+ | xxxx | +-------------+ 1 row in set (0.00 sec)
MIN()函數(shù)
mysql> SELECT MIN(f_price) AS min_price FROM fruits; +-----------+ | min_price | +-----------+ | 2.20 | +-----------+ 1 row in set (0.00 sec) mysql> SELECT s_id, MIN(f_price) AS min_price -> FROM fruits -> GROUP BY s_id; +------+-----------+ | s_id | min_price | +------+-----------+ | 101 | 3.20 | | 102 | 5.30 | | 103 | 2.20 | | 104 | 6.40 | | 105 | 2.60 | | 106 | 15.60 | | 107 | 3.60 | +------+-----------+ 7 rows in set (0.00 sec)
6.4、連接查詢
連接是關(guān)系數(shù)據(jù)庫(kù)模型的主要特點(diǎn)。連接查詢是關(guān)系數(shù)據(jù)庫(kù)中最主要的查詢。主要包括內(nèi)連接、外連接等。通過(guò)連接元算符可以實(shí)現(xiàn)多個(gè)表查詢。在關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)中,表建立時(shí)各個(gè)數(shù)據(jù)之間關(guān)系不確定,常把一個(gè)實(shí)體的所有信息存在一個(gè)表中。當(dāng)查詢時(shí),通過(guò)連接操作查詢出存在多個(gè)表中的不同實(shí)體的信息。
內(nèi)連接查詢
內(nèi)連接使用比較運(yùn)算符進(jìn)行表間某些列數(shù)據(jù)的比較操作,并列出這些表中與連接條件相匹配的數(shù)據(jù)行,組合成新記錄。
首先創(chuàng)建表與插入數(shù)據(jù):
mysql> CREATE TABLE suppliers -> ( -> s_id int NOT NULL AUTO_INCREMENT, -> s_name char(50) NOT NULL, -> s_city char(50) NULL, -> s_zip char(10) NULL, -> s_call CHAR(50) NOT NULL, -> PRIMARY KEY (s_id) -> ) ; Query OK, 0 rows affected (0.03 sec) mysql> INSERT INTO suppliers(s_id, s_name,s_city, s_zip, s_call) -> VALUES(101,'FastFruit Inc.','Tianjin','300000','48075'), -> (102,'LT Supplies','Chongqing','400000','44333'), -> (103,'ACME','Shanghai','200000','90046'), -> (104,'FNK Inc.','Zhongshan','528437','11111'), -> (105,'Good Set','Taiyuang','030000', '22222'), -> (106,'Just Eat Ours','Beijing','010', '45678'), -> (107,'DK Inc.','Zhengzhou','450000', '33332'); Query OK, 7 rows affected (0.01 sec) Records: 7 Duplicates: 0 Warnings: 0
在fruits表和suppliers表之間使用內(nèi)連接查詢
mysql> SELECT suppliers.s_id, s_name,f_name, f_price -> FROM fruits ,suppliers -> WHERE fruits.s_id = suppliers.s_id; +------+----------------+------------+---------+ | s_id | s_name | f_name | f_price | +------+----------------+------------+---------+ | 101 | FastFruit Inc. | apple | 5.20 | | 103 | ACME | apricot | 2.20 | | 101 | FastFruit Inc. | blackberry | 10.20 | | 104 | FNK Inc. | berry | 7.60 | | 107 | DK Inc. | xxxx | 3.60 | | 102 | LT Supplies | orange | 11.20 | | 105 | Good Set | melon | 8.20 | | 101 | FastFruit Inc. | cherry | 3.20 | | 104 | FNK Inc. | lemon | 6.40 | | 106 | Just Eat Ours | mango | 15.60 | | 105 | Good Set | xbabay | 2.60 | | 105 | Good Set | xxtt | 11.60 | | 103 | ACME | coconut | 9.20 | | 102 | LT Supplies | banana | 10.30 | | 102 | LT Supplies | grape | 5.30 | | 107 | DK Inc. | xbababa | 3.60 | +------+----------------+------------+---------+ 16 rows in set (0.00 sec) mysql> SELECT suppliers.s_id, s_name,f_name, f_price -> FROM fruits INNER JOIN suppliers -> ON fruits.s_id = suppliers.s_id; +------+----------------+------------+---------+ | s_id | s_name | f_name | f_price | +------+----------------+------------+---------+ | 101 | FastFruit Inc. | apple | 5.20 | | 103 | ACME | apricot | 2.20 | | 101 | FastFruit Inc. | blackberry | 10.20 | | 104 | FNK Inc. | berry | 7.60 | | 107 | DK Inc. | xxxx | 3.60 | | 102 | LT Supplies | orange | 11.20 | | 105 | Good Set | melon | 8.20 | | 101 | FastFruit Inc. | cherry | 3.20 | | 104 | FNK Inc. | lemon | 6.40 | | 106 | Just Eat Ours | mango | 15.60 | | 105 | Good Set | xbabay | 2.60 | | 105 | Good Set | xxtt | 11.60 | | 103 | ACME | coconut | 9.20 | | 102 | LT Supplies | banana | 10.30 | | 102 | LT Supplies | grape | 5.30 | | 107 | DK Inc. | xbababa | 3.60 | +------+----------------+------------+---------+ 16 rows in set (0.00 sec)
如果在一個(gè)連接查詢中,設(shè)計(jì)的兩個(gè)表是同一張表,這種查詢稱為字連接查詢。例如:查詢供應(yīng)f_id= ‘a(chǎn)1’的水果供應(yīng)商提供的其他水果種類。
mysql> SELECT f1.f_id, f1.f_name -> FROM fruits AS f1, fruits AS f2 -> WHERE f1.s_id = f2.s_id AND f2.f_id = 'a1'; +------+------------+ | f_id | f_name | +------+------------+ | a1 | apple | | b1 | blackberry | | c0 | cherry | +------+------------+ 3 rows in set (0.00 sec)
外間接查詢
外連接查詢將查詢多個(gè)表中相關(guān)聯(lián)的行,其分為左外連接和右外連接
左外連接:返回包括左表中的所有記錄和右表中連接字段相等的記錄
首先創(chuàng)建表和數(shù)據(jù):
mysql> CREATE TABLE orders  分享文章:六、MySQL查詢數(shù)據(jù)
文章URL:http://aaarwkj.com/article8/ipohip.html成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、微信小程序、做網(wǎng)站、網(wǎng)站改版、自適應(yīng)網(wǎng)站、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)