Reputation: 422
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
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
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
Reputation: 13700
Try this
$sql_data = "UPDATE `database1`.`users` SET `balance` = MIN(`balance`) + 5
WHERE ('" . $session->value('user_id') . "') "
Upvotes: 1
Reputation: 798814
Aggregate functions require an aggregation caused by a GROUP BY
clause. Without one, the query is invalid.
Upvotes: 1