如果我們想要在 SELECT 查詢中對非分組欄位使用分組函數,則必須使用 GROUP BY 子句。一般語法可以如下
SELECT group_function1,…, non-group-column1,… from table_name GROUP BY column_name;
mysql> Select COUNT(*), id from Student GROUP BY id; +----------+------+ | COUNT(*) | id | +----------+------+ | 1 | 1 | | 1 | 2 | | 1 | 15 | | 1 | 17 | | 1 | 20 | +----------+------+ 5 rows in set (0.00 sec) mysql> Select COUNT(*), address from Student GROUP BY id; +----------+---------+ | COUNT(*) | address | +----------+---------+ | 1 | Delhi | | 1 | Mumbai | | 1 | Delhi | | 1 | Shimla | | 1 | Jaipur | +----------+---------+ 5 rows in set (0.00 sec)
GROUP BY 子句後的欄位可以與 SELECT 查詢中給出的非群組欄位不同。
以上是我們如何在 MySQL SELECT 查詢中將群組函數與非群組欄位一起使用?的詳細內容。更多資訊請關注PHP中文網其他相關文章!