1

i want to UPDATE the a column after i SELECT a table

SELECT id_copies, id_shop, id_dvd 
FROM dvd_copies
WHERE id_dvd = '001-192.168.1.103-6' AND id_shop='002'
ORDER BY id_copies DESC
LIMIT 2;

i got just 2 rows that i want to be updated how can i UPDATE these rows?

Altiano Gerung
  • 824
  • 2
  • 15
  • 28

2 Answers2

4

You can use a nested select in your update query,note it will update your rows with same value not with a different value for each row

UPDATE dvd_copies 
SET your_column_to_update ='your value'
WHERE id_copies IN(
    SELECT t.id_copies FROM 
    (SELECT id_copies
        FROM dvd_copies 
        WHERE id_dvd = '001-192.168.1.103-6' AND id_toko='002'
        ORDER BY id_copies DESC LIMIT 2
    ) t
)
Sufian
  • 6,405
  • 16
  • 66
  • 120
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
3

You can also do the following:

UPDATE dvd_copies
SET your_column_to_update ='your value'
WHERE id_dvd = '001-192.168.1.103-6' AND id_toko='002'
ORDER BY id_copies DESC
LIMIT 2
vhu
  • 12,244
  • 11
  • 38
  • 48
  • This was very useful - i just forget that i can use LIMIT option in update query. That with order by keyword can complete relatively complex task with easy :) Thank you – xentia Oct 13 '15 at 15:03