SELECT and UPDATE statements one after other

Ashutosh

I am working on an Online Booking System. In a normal process when a user selects a room which was marked as 'Available' at a particular time and submits his choice. Now at back-end, just to confirm, I run a SELECT statement to verify the room's status is available or not as follows:

SELECT room_status FROM rooms WHERE room_number = 'XXX'

then I run a UPDATE query to mark it as 'UNAVAILABLE':

UPDATE rooms SET room_status = 'UNAVAILABLE' WHERE room_number = 'XXX' AND room_status = 'AVAILABLE'

Now when I ran SELECT query, the room was available and as I proceed to UPDATE query, the room is taken by other user. So the UPDATE query will fail. I want the room to be taken by first user only. What should I do ?

UPDATE: There can also be a case when a user has to book multiple rooms. For instance user selects 8 rooms. My code was at 4th room checking its status and as it reached 8th room the first room is gone. Also it may happen, while I was checking 4th room and upto now 8th room is gone.

I am using : PHP and MySQL

Stefano

When selecting the rows, you need to lock them, so that other queries can't read or write from them until you finish updating them. In MySQL, this can be done inside a transaction using the SELECT ... FOR UPDATE statement:

START TRANSACTION;
SELECT room_status FROM rooms WHERE room_number = 'XXX' FOR UPDATE;

All the rows returned from the previous statement will be locked until the end of the current transaction. You can now mark them as unavailable:

UPDATE rooms SET room_status = 'UNAVAILABLE' WHERE room_number = 'XXX';

Remember to close the transaction by commiting the changes. This will release the lock on the rows.

COMMIT;        

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Does JDBC's executeBatch() execute statements in parallel or one after the other

From Dev

Two SELECT statements in one stored procedure, one supplying input for another and the other returning more than one row

From Dev

Sum one table and update other with result, or just do `sum` on select?

From Dev

How to Update Two table Data one after other In codeigniter?

From Dev

How to Update Two table Data one after other In codeigniter?

From Dev

Select multiple rows into a single row one after the other in Postgres

From Dev

UIPickerView: select one element after the other finishes animation

From Dev

How to Stack mysql query results to select one row after the other

From Dev

Given two SELECT statements, how to merge them to get one result minus the other?

From Dev

Meteor reactive-var update one select element based on choice in other select element

From Dev

Meteor reactive-var update one select element based on choice in other select element

From Dev

SELECT statements as INSERT parameters along with other parameters

From Dev

How can I automatically update all other rows in the table after insert one new one in sql?

From Dev

Combine update and select statements containing join

From Dev

Update multiple rows using select statements

From Dev

Combine two different select and update statements

From Dev

Excel VBA - Select Case ends select after finding one case true and doesn't complete other cases

From Dev

Update statements with a one-to-many join

From Dev

How to create two update statements in one query?

From Dev

Combine three SQL update statements into one

From Dev

Combine multiple sql update statements into one

From Dev

SQL: Two select statements in one query

From Dev

Merge/Combine two select statements into one

From Dev

Joining mutiple select statements in one SQL statement

From Dev

MySQL multiple SELECT statements in one query

From Dev

Combining Two Select Sum Statements Into One

From Dev

SQL Server : multiple select statements on one column

From Dev

SELECT statements with one or more NOT EXISTS clauses

From Dev

Merge/Combine two select statements into one

Related Related

  1. 1

    Does JDBC's executeBatch() execute statements in parallel or one after the other

  2. 2

    Two SELECT statements in one stored procedure, one supplying input for another and the other returning more than one row

  3. 3

    Sum one table and update other with result, or just do `sum` on select?

  4. 4

    How to Update Two table Data one after other In codeigniter?

  5. 5

    How to Update Two table Data one after other In codeigniter?

  6. 6

    Select multiple rows into a single row one after the other in Postgres

  7. 7

    UIPickerView: select one element after the other finishes animation

  8. 8

    How to Stack mysql query results to select one row after the other

  9. 9

    Given two SELECT statements, how to merge them to get one result minus the other?

  10. 10

    Meteor reactive-var update one select element based on choice in other select element

  11. 11

    Meteor reactive-var update one select element based on choice in other select element

  12. 12

    SELECT statements as INSERT parameters along with other parameters

  13. 13

    How can I automatically update all other rows in the table after insert one new one in sql?

  14. 14

    Combine update and select statements containing join

  15. 15

    Update multiple rows using select statements

  16. 16

    Combine two different select and update statements

  17. 17

    Excel VBA - Select Case ends select after finding one case true and doesn't complete other cases

  18. 18

    Update statements with a one-to-many join

  19. 19

    How to create two update statements in one query?

  20. 20

    Combine three SQL update statements into one

  21. 21

    Combine multiple sql update statements into one

  22. 22

    SQL: Two select statements in one query

  23. 23

    Merge/Combine two select statements into one

  24. 24

    Joining mutiple select statements in one SQL statement

  25. 25

    MySQL multiple SELECT statements in one query

  26. 26

    Combining Two Select Sum Statements Into One

  27. 27

    SQL Server : multiple select statements on one column

  28. 28

    SELECT statements with one or more NOT EXISTS clauses

  29. 29

    Merge/Combine two select statements into one

HotTag

Archive