mysql 高階操作
阿新 • • 發佈:2018-11-08
-- -------------------------------------------------- -- mysql 函式 -- SOUNDEX() 轉換為語音值,可以將一個字串轉換為描述其語音表示的字母數字模式 SELECT * FROM USER WHERE SOUNDEX(username) = SOUNDEX('rot'); -- NOW()返回當前系統時間 SELECT NOW(); -- AVG() 返回某列的平均值 SELECT AVG(DISTINCT prod_price) AS avg_prod_price FROM products -- ------------------------------------- -- ------------------------------------ -- ORDER BY 排序 -- 先按prod_price降序,再按prod_name 升序 SELECT * FROM products ORDER BY prod_price DESC, prod_name ASC; -- GROUP BY 分組 -- GROUP BY 可以按一列或多列進行分組 -- GROUP BY 按分組欄位進行排序後,ORDER BY 可以以彙總欄位來進行排序。 SELECT prod_id, prod_name, COUNT(vend_id) AS vend_id FROM products GROUP BY vend_id; -- 分組後排序 SELECT prod_id, prod_name, COUNT(vend_id) AS vend_id FROM products GROUP BY vend_id ORDER BY prod_id DESC; -- -------------------------------------------------- /* HAVING 用於對彙總的 GROUP BY 結果進行過濾。 HAVING 要求存在一個 GROUP BY 子句。 WHERE 和 HAVING 可以在相同的查詢中。 HAVING vs WHERE WHERE 和 HAVING 都是用於過濾。 HAVING 適用於彙總的組記錄;而 WHERE 適用於單個記錄 */ SELECT prod_id, prod_name, COUNT(vend_id) AS num FROM products WHERE prod_name IS NOT NULL GROUP BY vend_id HAVING COUNT(vend_id) >= 2;