在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。
CREATE TABLE IF NOT EXISTS `sc`( `id` INT UNSIGNED, `scid` INT UNSIGNED )ENGINE=InnoDB DEFAULT CHARSET=utf8; insert into `sc`(id, scid) values (1,1),(2,1),(3,1),(2,2),(3,3); select * from sc;
SELECT id FROM SC GROUP BY id;
SELECT id FROM SC order BY id;
SELECT id FROM SC group BY id HAVING COUNT(*)> 1;id已经被查询过了
SELECT id FROM SC group BY id where COUNT(*)> 1;
在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。 HAVING 子句可以让我们筛选分组后的各组数据。 SQL HAVING语法如下: SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value;