I'm pretty good with mySQL but for some reason I'm struggling to get my head around this one.
I need to programmatically update the order values in the wp_esp_ticket table as the dates get out of order sometimes. This is the query I have to use to bring back all the dates that are related to each other and need ordering:
SELECT * FROM `wp_esp_ticket` et
INNER JOIN wp_esp_datetime_ticket edtt on edtt.TKT_ID = et.TKT_ID
INNER JOIN wp_esp_datetime edt on edtt.DTT_ID = edt.DTT_ID
WHERE edt.EVT_ID = 1325
This brings back all the rows that need updating in the correct order, but the TKT_order column in the wp_esp_ticket table is then out of sequence. I need to give the top row a value of 1 in the column TKT_order and go up in increments of 1 for each row below.
I need to put together a tidy sql statement for this as it will need to be run along with another peice of code that adds new dates to the database.
Thanksss!!!
Update:
Thanks to the input and direction from Drew I got this to work:
SET @newnum = 0;
Update wp_esp_ticket tix
INNER JOIN (
SELECT TKT_ID FROM `wp_esp_ticket` et
INNER JOIN wp_esp_datetime_ticket edtt on edtt.TKT_ID = et.TKT_ID
INNER JOIN wp_esp_datetime edt on edtt.DTT_ID = edt.DTT_ID
WHERE edt.EVT_ID = 1325
ORDER BY edt.DTT_EVT_start ASC
) b ON tix.TKT_ID = b.TKT_ID
SET tix.TKT_order = @newnum:=@newnum + 1