Lorn Lorn - 1 month ago 5
SQL Question

SQL How to Update only first Row

I'm working with PostgreSQL.
I have table with some elements. In last column there is 'Y' or 'N' letter. I need command which Select only first that match (I mean where last column is 'N') and change it on 'Y'.

My idea:

UPDATE Table SET Checked='Y'
WHERE (SELECT Checked FROM Table WHERE Checked='N' ORDER BY ID LIMIT 1) = 'N'


But it changes 'N' to 'Y' in every row.

Answer

Here is query

UPDATE Table SET Checked='Y' 
WHERE ID =(SELECT ID FROM Table WHERE Checked='N' ORDER BY ID LIMIT 1) 
Comments