获得至少3个项目的所有帐户

时间:2015-10-14 05:39:17

标签: mysql sql

我试图获得至少有3个项目的所有帐户。

我决定从链接表开始:

SELECT   
        accounts_project_1accounts_ida
FROM    accounts_project_1_c 

GROUP BY accounts_project_1accounts_ida
HAVING   COUNT(accounts_project_1accounts_ida) > 3

但它会返回所有表格,即使是只有一个项目的帐户。

例如,此查询仅返回1个结果:

SELECT * FROM `accounts_project_1_c` WHERE `accounts_project_1accounts_ida` LIKE '25490fce-5354-d1a2-fbe7-541b6c5b57d0'

这是表结构:

enter image description here

你知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

You HAVING子句不正确。它应该是

HAVING COUNT(accounts_project_1project_idb) > 3

从您的架构中,我了解每个项目都有一个ID accounts_project_1project_idb和一个与之关联的帐户。该帐户的ID是accounts_project_1accounts_ida。因此,您需要按照帐户ID的记录进行分组,然后选择至少包含三个项目的所有项目(表格中有三条记录)。话虽这么说,我想如果你把你的HAVING条款改为上面那个,你就会得到你想要的。

SELECT   
        accounts_project_1accounts_ida,
        COUNT(accounts_project_1project_idb)
FROM    accounts_project_1_c 

GROUP BY accounts_project_1accounts_ida
HAVING COUNT(accounts_project_1project_idb) > 3