如果列的数量超过一定量,如何仅检索数据

时间:2013-11-09 14:03:55

标签: mysql sql

这是我的查询

SELECT news.*, count(followers.artist_id) as followers 
FROM news 
INNER JOIN followers ON news.artist_id = followers.artist_id 
GROUP BY followers.artist_id 
ORDER BY followers DESC

因此,我目前正在获取特定艺术家的追随者数量所订购的所有新闻。

我想要做的只是在追随者超过一定数量时才检索新闻,比如10。

所以我试着抛出一个where子句

WHERE followers > 10

但是我得到一个错误,即where子句中没有这样的列。

我能做些什么来实现这个目标?我可以制作一个包含追随者数量的假列吗?

干杯

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT news.*, x.[followers]
FROM news
INNER JOIN (
    SELECT news.id, Count(followers.id) as [followers]
    FROM news 
    INNER JOIN followers ON news.artist_id = followers.artist_id 
    GROUP BY news.id
    HAVING Count(followers.id) > 10) as x on x.id = news.id

我假设两个表(新闻和关注者)都有 id 列,该列唯一标识该行。