使用COUNT和NOT IN查找总结果

时间:2014-03-05 02:50:58

标签: mysql

这是我当前使用的查询

SELECT * 
FROM accounts 
WHERE source = 0 
AND account_id NOT IN(SELECT receive FROM actions WHERE follow = '$account') 
AND status = 0 
ORDER BY RAND() LIMIT

我想以某种方式做这样的事情

SELECT * 
FROM accounts 
WHERE source = 0 
AND account_id NOT IN(SELECT * FROM actions WHERE follow = '$account') 
AND COUNT((SELECT * FROM actions WHERE follow = '$account')) AS `total_received_follows`
AND total_received_follows < max_follows
AND status = 0 
ORDER BY RAND() LIMIT

我基本上需要从COUNT表中获取follow值为'$account'的行的actions我想要检查此值是否较低值比max_follows

中的行accounts

我明白这可能不是正确的语法,但无论如何我可以使用我的查询做类似的事情吗?

1 个答案:

答案 0 :(得分:1)

我有点困惑,但是这里......在你的第二个查询中,你正在寻找actions中没有任何相应行的帐户?但是,您似乎还在尝试查找max_follows中行数少于actions的帐户。我对吗?你意识到这是矛盾的吗?

无论如何,这可能是一个开始的地方:

SELECT acc.*, COUNT(act.id) act_count FROM accounts acc
LEFT JOIN action act ON act.follow = acc.id
WHERE ...
GROUP BY acc.id
HAVING act_count < acc.max_follows