1. 程式人生 > >Group by的用法

Group by的用法

Group by的用法 GROUP BY 語句根據一個或多個列對結果集進行分組。在分組的列上我們可以使用 COUNT, SUM, AVG,等函式。 Group by用法: SELECT column_name, function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name; 分組後的條件使用 HAVING 來限定,WHERE 是對原始資料進行條件限制。幾個關鍵字的使用順序為 where 、group by 、having、order by ,
單個欄位場景: SELECT NAME, SUM (*) FROM employee_tbl WHERE ID <> 1 GROUP BY NAME HAVING SUM (*) > 5 ORDER BY SUM (*) DESC; 單個欄位場景: SELECT CONTENTID, LOGINNAME, "MAX" (CREATETIME) AS CREATETIME FROM USER_SCORE_RECORD
WHERE CONTENTID = 640658720 GROUP BY CONTENTID, LOGINNAME