space ranger
space ranger

Reputation: 422

Invalid use of group functions

What causes this error?:

Invalid use of group functions

Here is the sql code causing the error:

$sql_data = "UPDATE `database1`.`users` SET `balance` = MIN(`balance` + 5) 
WHERE ('" . $session->value('user_id') . "') ";

Upvotes: 0

Views: 159

Answers (4)

Omesh
Omesh

Reputation: 29101

I think this is what you are looking for:

UPDATE database1.users a
    INNER JOIN
        (SELECT id, MIN(balance)
         FROM database1.users
         WHERE user_id= ('" . $session->value('user_id') . "')
         GROUP BY id
        ) b
        ON a.id = b.id
WHERE user_id = ('" . $session->value('user_id') . "');

Upvotes: 0

rybosome
rybosome

Reputation: 5136

Without knowing what the error is, I'm assuming it is stemming from MIN(balance + 5). I think you mean MIN(balance) + 5 - MIN should only take a column name, not an arithmetic expression.

Upvotes: 0

Madhivanan
Madhivanan

Reputation: 13700

Try this

$sql_data = "UPDATE `database1`.`users` SET `balance` = MIN(`balance`) + 5  
WHERE ('" . $session->value('user_id') . "') "

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798814

Aggregate functions require an aggregation caused by a GROUP BY clause. Without one, the query is invalid.

Upvotes: 1

Related Questions