In a mySQL table inv
there are 3 columns : ID
, num
and state
.
state
has 3 values : 0 = unused, 1 = used, 2 = reserved.
To get the next unused num
I use this query :
SELECT num FROM inv WHERE state = 0
When the query returns an num
I need to update the record and set state
to 2 = reserved.
UPDATE inv SET state = 2 Where ID = id
I know there is a way to to do this in one query only, but how ?
Any helping answer is appreciated
EDIT :
I am trying to achive this :
I need to generate invoices with unique numbers. So I fetch the next free , unused number whith state = 0, then return this number and set in the same query the returned record to state = 2, so nobody else will use it in the meanwhile. Later when I succeed to generate a flawless invoice I update this record with state = 1 (used). In case generating an invoice flawless fails I set the state back to 0 = unused.
Hope I could express my approach more clearly.