I have two tables. ticket & ticketlist.
The sold out column in the ticket table needs to be 1 if that item is sold out.
Table ticket needs to be soldout when the count for that item in table ticketlist is 2.
ticket
ticketid, quantity, soldout
21 2 1
ticketlist
ticketlistid, ticketid
3 21
4 21
The logic is:
soldout should be ‘1’ if ticket.quantity – (COUNT(ticketlist.ticketlistid) WHERE ticket.ticketid = ticketlist.ticketlistid) > 0
This is the MySQL that I tried
UPDATE ticket
SET soldout = '1'
WHERE quantity - (SELECT ticket.ticketid, COUNT(ticketlist.ticketlistid)
FROM ticket, ticketlist
WHERE ticket.ticketid = ticketlist.ticketid) > '0';
Any help will be appreciated.
In your subselect:
You probably also want to set
sold_outto one whenquantity - (SELECT ...) <= 0, rather than> 0as you are currently doing.Change the query to this:
Also your database is denormalized. You are storing information in one table that can be derived from the data in another table. This redundancy can cause errors if the two ever get out of sync. I’d recommend only doing this if you need it for performance reasons.