本文實(shí)例講述了MySQL使用集合函數(shù)進(jìn)行查詢操作。分享給大家供大家參考,具體如下:
目前創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、含山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
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ì)不同訂單號(hào)中訂購的水果種類
SELECT o_num, COUNT(f_id) FROM orderitems GROUP BY o_num;
【例.37】在orderitems表中查詢30005號(hào)訂單一共購買的水果總量,輸入如下語句:
SELECT SUM(quantity) AS items_total FROM orderitems WHERE o_num = 30005;
【例.38】在orderitems表中,使用SUM()
函數(shù)統(tǒng)計(jì)不同訂單號(hào)中訂購的水果總量
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表中查找市場(chǎng)上價(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表中查找市場(chǎng)上價(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存儲(chǔ)過程技巧大全》及《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫計(jì)有所幫助。