我使用此查询获取至少3个项目的所有帐户。
SELECT
accounts.id,
accounts.name,
COUNT(accounts_project_1project_idb) as count
FROM accounts_project_1_c
LEFT JOIN accounts ON accounts_project_1accounts_ida = accounts.id
LEFT JOIN project ON accounts_project_1project_idb = project.id
LEFT JOIN project_cstm ON id_c = project.id
GROUP BY accounts_project_1accounts_ida
HAVING COUNT(accounts_project_1project_idb) >= 3
ORDER BY count DESC
其中accounts
是帐户表,project
是项目表,project_cstm
表包含有关项目的更多信息
此请求返回153结果。
但是现在,我希望将联系人链接到一个帐户。如果它存在多个联系人,我真的不在乎,我只想要一个。
SELECT
accounts.id,
accounts.name,
contacts.first_name,
contacts.last_name,
contacts.phone_mobile,
contacts.phone_work,
COUNT(accounts_project_1project_idb) as count
FROM accounts_project_1_c
LEFT JOIN accounts ON accounts_project_1accounts_ida = accounts.id
LEFT JOIN project ON accounts_project_1project_idb = project.id
LEFT JOIN project_cstm ON id_c = project.id
LEFT JOIN accounts_contacts ON accounts.id = accounts_contacts.account_id
LEFT JOIN contacts ON accounts_contacts.contact_id = contacts.id
GROUP BY accounts_project_1accounts_ida
HAVING COUNT(accounts_project_1project_idb) >= 3
ORDER BY count DESC
此请求返回173个结果。
我真的不明白,使用left join
它不应该添加任何行吗?
你能告诉我我做错了什么吗?
非常感谢。
答案 0 :(得分:1)
我认为您希望MarriageLicen
Year Month Amount
1 2011 Jan 742
2 2011 Feb 796
3 2011 Mar 1210
4 2011 Apr 1376
BusinessLicen
Month Year MARRIAGE_LICENSES
1 Jan 2011 754
2 Feb 2011 2706
3 Mar 2011 2689
4 Apr 2011 738
成为:
HAVING
您可能也想在HAVING COUNT(DISTINCT accounts_project_1project_idb) >= 3
中使用它。