我有以下sql表达式:
select id,count(oID) from MyTable group by oID;
我得到类似的东西
+-------+---------------+ | id | count(oID) | +-------+---------------+ | 002 | 6 | | 104 | 1 | | 101 | 1 | | 908 | 1 | +-------+---------------+
现在我想拉出count(oID)为1的行(select id).如何在一个sql语句中执行此操作?
解决方法
使用HAVING子句筛选聚合列.
SELECT id,count(oID) FROM MyTable GROUP BY oID HAVING count(oID) = 1
更新1
将结果包装在子查询中
SELECT a.* FROM tableName a INNER JOIN ( SELECT id FROM MyTable GROUP BY id HAVING count(oID) = 1 ) b ON a.ID = b.ID