您好,登錄后才能下訂單哦!
本文實例講述了MySQL使用集合函數進行查詢操作。分享給大家供大家參考,具體如下:
COUNT
函數
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
函數
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
函數
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
函數
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
函數
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表中總的行數
SELECT COUNT(*) AS cust_num from customers;
【例.35】查詢customers表中有電子郵箱的顧客的總數,輸入如下語句:
SELECT COUNT(c_email) AS email_num FROM customers;
【例.36】在orderitems表中,使用COUNT()
函數統計不同訂單號中訂購的水果種類
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()
函數統計不同訂單號中訂購的水果總量
SELECT o_num, SUM(quantity) AS items_total FROM orderitems GROUP BY o_num;
【例.39】在fruits表中,查詢s_id=103的供應商的水果價格的平均值,SQL語句如下:
SELECT AVG(f_price) AS avg_price FROM fruits WHERE s_id = 103;
【例.40】在fruits表中,查詢每一個供應商的水果價格的平均值,SQL語句如下:
SELECT s_id,AVG(f_price) AS avg_price FROM fruits GROUP BY s_id;
【例.41】在fruits表中查找市場上價格最高的水果,SQL語句如下:
mysql>SELECT MAX(f_price) AS max_price FROM fruits;
【例7.42】在fruits表中查找不同供應商提供的價格最高的水果
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表中查找市場上價格最低的水果,SQL語句如下:
mysql>SELECT MIN(f_price) AS min_price FROM fruits;
【例.45】在fruits表中查找不同供應商提供的價格最低的水果
SELECT s_id, MIN(f_price) AS min_price FROM fruits GROUP BY s_id;
更多關于MySQL相關內容感興趣的讀者可查看本站專題:《MySQL常用函數大匯總》、《MySQL日志操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數據庫鎖相關技巧匯總》
希望本文所述對大家MySQL數據庫計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。