How to update the first record in a table using mysql?

I have an unknown number of entries in the table "room_photos". There is a column called "main_photo", and currently each entry has a value of "no" for this column. I would like to create a sql statement that updates the first record in the table and changes the value of the "main_photo" column to "yes".

So this is what I have now:

TABLE room_photos
photo_id     main_photo
"" nbsp; ; | no

And this is what I need:

TABLE room_photos
photo_id     main_photo
                                        "nbsp ;; | no

+5
source share
1 answer

Use LIMIT

UPDATE tablename SET main_photo = 'yes' LIMIT 1;

The query above assumes that the first record in the table, regardless of the value of photo_id, will be updated. If you want the record with the lowest identifier to be updated, also use ORDER BY:

UPDATE tablename SET main_photo = 'yes' ORDER BY photo_id ASC LIMIT 1;
+29
source

All Articles