问题描述:

i try to select all users who have a higher document size then 50.

So i group the size column in the database, but i get this error: "Invalid use of group function"

What do i do wrong?

$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());

网友答案:

You want to use a HAVING clause after the group by.

So your query might look like

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'";
相关阅读:
Top