I have such a table, with dates in format YYYY-MM-DD
id | state_id | created_on | closed_on |
1 | 1 | 2020-02-19 | NULL |
2 | 2 | 2020-01-02 | 2020-01-03 |
3 | 1 | 2020-01-05 | NULL |
4 | 4 | 2020-02-07 | 2020-02-08 |
4 | 3 | 2020-02-20 | NULL |
I need to update the STATE with a random state between 2 and 6 WHERE
- state_id is 1 OR state_id is 3
- created_on is in the past
I can do so with this (IT WORKS PERFECTLY):
UPDATE table SET state_id = FLOOR(2 + rand() * 6)
WHERE created_on < CURRENT_DATE()
AND (state_id=1 OR state_id=3)
What I need to do additionally in the same update, would be to set closed_on:
- NULL if the new random state is 3
CURRENT_DATE()
if the new random state IS anything but 3
I thought about using SET @newState = FLOOR(2 + rand() * 6)
, but this way all the records would have the same state_id
is it possible to update with different values?
something like this
UPDATE table SET state_id = FLOOR(2 + rand() * 6), closed_on = IF(new_state_id = 3, NULL, CURRENT_DATE())
WHERE created_on < CURRENT_DATE()
AND (state_id=1 OR state_id=3)
Aucun commentaire:
Enregistrer un commentaire