按组大小分组无效使用组功能

时间:2014-02-12 22:27:35

标签: mysql group-by

我尝试选择文档大小超过50的所有用户。 所以我将数据库中的size列分组,但是我收到此错误:“组函数的使用无效” 我做错了什么?

$sql_select_document = "SELECT gebruikers.id AS userId, 
                                        documenten.upload_user_id AS uuid
            FROM documenten 
            JOIN gebruikers
            ON documenten.upload_user_id = gebruikers.id
        WHERE SUM(documenten.size) > 50
            AND gebruikers.id = '".$fetch_users['id']."'";

$query_select_document = mysql_query($sql_select_document) or die(mysql_error());

1 个答案:

答案 0 :(得分:3)

您希望在分组后使用HAVING子句。

所以你的查询可能看起来像

SELECT gebruikers.id AS userId, 
       documenten.upload_user_id AS uuid
            FROM documenten 
            JOIN gebruikers
            ON documenten.upload_user_id = gebruikers.id
        WHERE gebruikers.id = '".$fetch_users['id']."'.
GROUP BY gebruikers.id, documenten.upload_user_id
   HAVING SUM(documenten.size) > 50'";