Mixing of GROUP columns (MIN(),MAX(),COUNT(),…) with no GROUP columns is illegal if there is no GROUP BY clause

前端 未结 1 1417
名媛妹妹
名媛妹妹 2021-01-24 09:51

I\'m using an old php script and have an error with this query. Since I\'m not experienced with mysql, I couldn\'t fix it.

\"SELECT COUNT(p.postid) AS pid, p.*,         


        
相关标签:
1条回答
  • 2021-01-24 10:46

    Like the error says, you can't SELECT an aggregate function, such as COUNT, without grouping rows either explicitly (using GROUP BY) or implicitly (by just selecting the aggregate). To put it in less technical terms - you're telling the database, "Look up all posts by this username, and the threads they belong to, and the number of posts", and the database is answering you, "the number of posts in what?".

    So you'll need to be more specific. If what you actually want is:

    • If you don't actually care about all the individual posts, and you just want the threads and the number of posts by this user per thread, remove the p.* from the SELECT, and add GROUP BY t.threadid to the end of the query.
    • If you want the total number of posts as well as all the posts and threads, just count the result rows.
    • If you want the total number of posts by all users in the threads you're selecting, that'll make the query much more complex, because you'll need to join on post again to get the total post count.
    • If you don't actually want the count at at all, remove it from the query. :)
    0 讨论(0)
提交回复
热议问题