如果我们想在 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 查询中给出的非组字段不同。
原文来自:www.php.cn© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容