Mysql 如何仅在列的计数超过一定数量时检索数据
这是我的问题Mysql 如何仅在列的计数超过一定数量时检索数据,mysql,sql,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个,我只想检索新
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子句中没有这样的列
我能做些什么来实现这一点?我可以制作一个包含跟随者计数的假列吗
干杯试试这个:
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列。使用
HAVING
。例如,分组依据。。。。拥有数量(追随者.艺术家id)>10….