内连接中的where子句不起作用

时间:2014-08-26 00:00:48

标签: mysql

我有以下sql查询:

select count(tu.authorID) as c,t.tweettext,tu.name 
from tweet t 
inner join tweet_user tu on t.authorid=tu.authorid 
where count(tu.authorID)>=5;

但似乎内部联接不适用于本案例中的位置,我收到以下错误:

Error Code: 1111. Invalid use of group function 0.062 sec

有谁知道我的问题是什么,或者我怎么做?

2 个答案:

答案 0 :(得分:1)

对于group子句,您需要使用

GROUP BY x
HAVING COUNT(x) > 0

http://www.w3schools.com/sql/sql_having.asp

答案 1 :(得分:1)

使用HAVING而不是WHERE作为COUNT等汇总功能:

 SELECT t.tweettext,tu.name, count(tu.authorID) as c 
 FROM tweet t inner join tweet_user tu on t.authorid=tu.authorid 
 HAVING count(tu.authorID)>=5 

尽管mySQL可以取消GROUP BY,但您可能也可以包含GROUP BY,但我认为您不需要tweettext

 SELECT tu.name, count(tu.authorID) as c 
 FROM tweet t inner join tweet_user tu on t.authorid=tu.authorid 
 GROUP BY tu.name
 HAVING count(tu.authorID)>=5