1

I have following table:

       TableA
  x               y
----------------------
 3                1
 2                5
 5                2

Now i want to update the value in column x, where the value in column y is lowest, something like that:

UPDATE TableA SET x = 1 WHERE y = min(y);

Then the first row should be x = 1 and y = 1.

But i get following error code:

Error Code: 1111. Invalid use of group function

How can i fix that?

Jariel
  • 79
  • 6

3 Answers3

1

If y is unique, then you can use order by and limit:

UPDATE TableA
    SET x = 1 
    ORDER BY Y ASC
    LIMIT 1;

If you have multiple rows with the minimum value, you can use join:

UPDATE TableA a JOIN
       (SELECT MIN(Y) as miny FROM TableA) m
       ON a.y = m.miny
    SET x = 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

you can write your query as follow:

UPDATE TableA SET x = 1 WHERE y IN (select min(y) from TableA T2);
Mahmoud
  • 883
  • 7
  • 17
0
UPDATE TableA
    JOIN (SELECT min(y) as min_y, FROM TableA GROUP BY y) 
      as temp ON temp.min_y = TableA.y
   SET x = 1;
Gravy
  • 12,264
  • 26
  • 124
  • 193