我正在使用以下查询成功连接两个表。我想添加标准,仅显示至少有2条记录的代理,其中SearchPrice> = 250000 ...我尝试添加“HAVING COUNT()> = 2”但是当我添加此项时,我只会获得一个结果?我认为它可能会对ActiveAgent表应用“HAVING COUNT()> = 2”吗?
SELECT
r.EmailAddress AS ag_email,
e.ListingAgentFullName AS ag_name,
r.OfficeName AS ag_office_name
FROM RESI
e JOIN ActiveAgent r
ON e.ListingAgentNumber=r.MemberNumber
WHERE SearchPrice >= 250000;
答案 0 :(得分:1)
试试这个
SELECT
r.EmailAddress AS ag_email,
COUNT(r.EmailAddress) AS `acount`,
e.ListingAgentFullName AS ag_name,
r.OfficeName AS ag_office_name
FROM RESI
e LEFT JOIN ActiveAgent r
ON e.ListingAgentNumber=r.MemberNumber
WHERE SearchPrice >= 250000;
GROUP BY r.EmailAddress
HAVING acount >=2