Pregunta

Suppose I have the following table structure

 ________________________
|      |         |       |         
|Col1  |  Col2   |  Col3 | 
|______|_________|_______|
|      |         |       |         
| 123  |   245   |  aaa  | 
|______|_________|_______|
|      |         |       |         
| 123  |  245    |  bbb  | 
|______|_________|_______|

I need to update the first row but not the second, but I don't want to reference Col3.

If I do

 UPDATE table SET col1 = '789' WHERE col2='245'

It will update both rows. How could I craft the above query to only update the FIRST record it finds if the WHERE clause is to be kept at col2='245'?

¿Fue útil?

Solución

If you're not ordering, there is no first row to update, it will just "pull one out of the available ones".

If you're clear about a pseudorandom one of the two rows will be updated, you can use LIMIT with the update;

UPDATE mytable SET col1 = '789' WHERE col2='245' LIMIT 1;

An SQLfiddle to test with.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top