Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update row with minimum value sql

Tags:

sql

mysql

I have this table:

-----------------------
summonerId | timestamp
-----------------------
253222     | 14395235091096
929112     | 14395235091056
(...)

I want to update the row with the lower timestamp but I can't, when I do this

UPDATE summoners_shell 
SET 
summonerId = ".$s.",
timestamp = ".$time." 
WHERE timestamp = (SELECT MIN(timestamp))

It updates all rows! Why? How do I do what I want?

like image 343
user3820203 Avatar asked Aug 14 '15 03:08

user3820203


1 Answers

When SELECT-subquery is in WHERE-clause, it locks the table so an update can not pass. Just use JOIN instead

UPDATE summoners_shell AS s1
JOIN (SELECT MIN(timestamp) AS mt FROM summoners_shell) AS mt ON (s1.timestamp = mt.mt)
SET 
    summonerId = ".$s.",
    timestamp = ".$time."
like image 190
M0rtiis Avatar answered Sep 22 '22 19:09

M0rtiis