本文實(shí)例講述了MySQL使用集合函數(shù)進(jìn)行查詢操作。分享給大家供大家參考,具體如下:
COUNT
函數(shù)
SELECT COUNT(*) AS cust_num from customers;
SELECT COUNT(c_email) AS email_num FROM customers;
SELECT o_num, COUNT(f_id) FROM orderitems GROUP BY o_num;
SUM
函數(shù)
SELECT SUM(quantity) AS items_total FROM orderitems WHERE o_num = 30005;
SELECT o_num, SUM(quantity) AS items_total FROM orderitems GROUP BY o_num;
AVG
函數(shù)
SELECT AVG(f_price) AS avg_price FROM fruits WHERE s_id = 103;
SELECT AVG(f_price) AS avg_price FROM fruits group by s_id;
MAX
函數(shù)
SELECT MAX(f_price) AS max_price FROM fruits;
SELECT s_id, MAX(f_price) AS max_price FROM fruits GROUP BY s_id;
SELECT MAX(f_name) from fruits;
MIN
函數(shù)
SELECT MIN(f_price) AS min_price FROM fruits;
SELECT s_id, MIN(f_price) AS min_price FROM fruits GROUP BY s_id;
【例.34】查詢customers表中總的行數(shù)
SELECT COUNT(*) AS cust_num from customers;
【例.35】查詢customers表中有電子郵箱的顧客的總數(shù),輸入如下語句:
SELECT COUNT(c_email) AS email_num
FROM customers;
【例.36】在orderitems表中,使用COUNT()
函數(shù)統(tǒng)計(jì)不同訂單號中訂購的水果種類
SELECT o_num, COUNT(f_id) FROM orderitems GROUP BY o_num;
【例.37】在orderitems表中查詢30005號訂單一共購買的水果總量,輸入如下語句:
SELECT SUM(quantity) AS items_total
FROM orderitems
WHERE o_num = 30005;
【例.38】在orderitems表中,使用SUM()
函數(shù)統(tǒng)計(jì)不同訂單號中訂購的水果總量
SELECT o_num, SUM(quantity) AS items_total
FROM orderitems
GROUP BY o_num;
【例.39】在fruits表中,查詢s_id=103的供應(yīng)商的水果價(jià)格的平均值,SQL語句如下:
SELECT AVG(f_price) AS avg_price
FROM fruits
WHERE s_id = 103;
【例.40】在fruits表中,查詢每一個(gè)供應(yīng)商的水果價(jià)格的平均值,SQL語句如下:
SELECT s_id,AVG(f_price) AS avg_price
FROM fruits
GROUP BY s_id;
【例.41】在fruits表中查找市場上價(jià)格最高的水果,SQL語句如下:
mysql>SELECT MAX(f_price) AS max_price FROM fruits;
【例7.42】在fruits表中查找不同供應(yīng)商提供的價(jià)格最高的水果
SELECT s_id, MAX(f_price) AS max_price
FROM fruits
GROUP BY s_id;
【例.43】在fruits表中查找f_name的最大值,SQL語句如下
SELECT MAX(f_name) from fruits;
【例.44】在fruits表中查找市場上價(jià)格最低的水果,SQL語句如下:
mysql>SELECT MIN(f_price) AS min_price FROM fruits;
【例.45】在fruits表中查找不同供應(yīng)商提供的價(jià)格最低的水果
SELECT s_id, MIN(f_price) AS min_price
FROM fruits
GROUP BY s_id;
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計(jì)有所幫助。
您可能感興趣的文章:- Mysql常用函數(shù)大全(分類匯總講解)
- MySQL自定義函數(shù)簡單用法示例
- MYSQL自定義函數(shù)判斷是否正整數(shù)的實(shí)例代碼
- MySQL中的排序函數(shù)field()實(shí)例詳解
- MySQL高效模糊搜索之內(nèi)置函數(shù)locate instr position find_in_set使用詳解
- mysql中find_in_set()函數(shù)的使用及in()用法詳解
- 詳解MySql Date函數(shù)
- 詳解數(shù)據(jù)庫_MySQL: mysql函數(shù)