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.
To return only the first row that matches your SELECT query, you need to add the LIMIT clause to your SELECT statement. The LIMIT clause is used to control the number of rows returned by your query. When you add LIMIT 1 to the SELECT statement, then only one row will be returned.
UPDATE TOP (100) table_name set column_name = value; If you want to show the last 100 records, you can use this if you need. Show activity on this post. The TOP qualifier can also be used as limit the the number of rows manually updated incorrectly.
Here is query
UPDATE Table SET Checked='Y'
WHERE ID =(SELECT ID FROM Table WHERE Checked='N' ORDER BY ID LIMIT 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With