MySQL Forums
Forum List  »  Newbie

Re: conditional insert then query
Posted by: Felix Geerinckx
Date: May 30, 2005 02:43AM

jim tay wrote:

> update flightinfo set seatsAvailible = (seatsAvailible - 13)
> where source Airport = 'Dublin' and destAirport = 'London'
> and flightDate = '2005-05-23 08:30:00'
> and (seatsAvailible - 13) > 0;
>
> I'm just wondering would there be a more elegant way to do this?

This is the correct way to do it, but you have a bug: you will never fill the last seat.
Personally, I would write the last condition as follows:

AND seatsAvailable >= 13

(note the >=)

Also, you need to check wether your update succeeded (maybe another client has taken all the available seats in the meantime). In MySQL 5.x you can use the ROW_COUNT function to do that.

But I would use either transactions or table locking anyway for this kind of application.

--
felix
Please use BBCode to format your messages in this forum.

Options: ReplyQuote


Subject
Written By
Posted
May 28, 2005 03:04PM
Re: conditional insert then query
May 30, 2005 02:43AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.