我试图显示一个字段出现一定次数的行。假设下表:Campaign
+------------+-------------+--------------+-------- --------+ | account_id | 活动 ID | 帐户名 | 帐户用户 | +------------+-------------+--------------+-------- --------+ | 85 | 21 | 一个 | 36 | | 45 | 69 | 乙| 82 | | 78 | 78 | C | 28 | | 69 | 65 | D | 25 | | 85 | 87 | E | 96 | | 78 | 12 | F | 63 | | 78 | 78 | 克| 96 | +------------+-------------+--------------+-------- --------+
还有桌子Account
+------------+--------------+ | account_id | 帐户代码 | +------------+--------------+ | 85 | AAA | | 45 | 美国广播公司 | | 78 | RTY | | 69 | WER | | 85 | AAA | | 78 | RTY | | 78 | RTY | +------------+--------------+
我需要显示表中所有Campaign
带有 ( join
)account_code
的表列account
,仅用于那些在表中account_id
出现两次或多次的帐户campaign
。因此,结果应该是:
+------------+-------------+--------------+-------- ------+--------------+ | account_id | 活动 ID | 帐户名 | 帐户用户 | 帐户代码 | +------------+-------------+--------------+-------- ------+--------------+ | 85 | 21 | 一个 | 36 | AAA | | 78 | 78 | C | 28 | RTY | | 85 | 87 | E | 96 | AAA | | 78 | 12 | F | 63 | RTY | | 78 | 78 | 克| 96 | RTY | +------------+-------------+--------------+-------- ------+--------------+
我尝试使用GROUP BY
andHAVING
子句,但它似乎不起作用:
SELECT c.account_id ,c.campaign_id ,c.account_name ,c.account_users ,a.account_code
FROM campaign c
INNER JOIN account a
GROUP BY c.account_id, c.campaign_id, c.account_name, c.account_users, a.account_code
HAVING count(*) >= 2;
我在这里错过了什么吗?