基本上我有一个主表(帐户)和一个元表(accounts_meta)...元表如下所示:
id | account_id | meta_key | meta_value
我想要做的只是在accounts_meta表中选择没有'referrer_paid'作为行的帐户...
到目前为止,这是我的代码......
SELECT a.* FROM accounts AS a
LEFT JOIN accounts_meta AS am ON a.id = am.account_id AND am.meta_key != 'referrer_paid'
WHERE a.account_referrer != ''
GROUP BY a.id
希望我有意义。我做错了什么?
答案 0 :(得分:4)
SELECT *
FROM accounts
WHERE id NOT IN ( select DISTINCT account_id
from `account_meta_table`
where meta_key != 'referrer_paid'
);
答案 1 :(得分:4)
SELECT * FROM accounts WHERE id NOT IN ( select account_id from `account_meta_table` where meta_key = 'referrer_paid' );
答案 2 :(得分:2)
SELECT a.* FROM accounts AS a
LEFT JOIN accounts_meta AS am ON a.id = am.account_id AND am.meta_key = 'referrer_paid'
WHERE a.account_referrer != ''
AND am.account_id IS NULL
你不需要group by,因为left-join-is-null不会产生重复的帐户行
编辑: duh,将am.meta_key != 'referrer_paid'
更改为am.meta_key = 'referrer_paid'
这就是你想要的。 如果连接的行不匹配,则返回NULL,并且只接受NULL行
答案 3 :(得分:0)
选择a。* FROM帐户AS a LEFT JOIN accounts_meta AS am ON a.id = am.account_id AND am.meta_key!='referrer_paid' 哪里有ISNULL(am.account_referrer) GROUP BY a.id