如果我们想在 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中文网其他相关文章!