I have a table called "scores" with following info:
| id | name | datetime_added | option |
+----+-------+---------------------+--------+
| 1 | all | 2023-01-01 00:10:04 | none |
+----+-------+---------------------+--------+
| 2 | James | 2023-01-01 00:32:18 | none |
+----+-------+---------------------+--------+
| 3 | all | 2023-01-01 02:08:51 | none |
+----+-------+---------------------+--------+
| 4 | all | 2023-01-01 04:13:07 | none |
+----+-------+---------------------+--------+
| 5 | Rubio | 2023-01-01 07:05:55 | none |
+----+-------+---------------------+--------+
| 6 | all | 2023-01-01 09:17:27 | none |
+----+-------+---------------------+--------+
| 7 | James | 2023-01-01 10:43:01 | none |
+----+-------+---------------------+--------+
For output I need to add option "some" to the newest entry of "Rubio" where option is "none" if there is any. If there isn’t (either name "Rubio" or option "none") then I need to add the option "some" to the newest entry of "all".
What I have done so far? I need to get that table sorted in two ways:
- By name, "Rubio" needs to be first and "all" needs to be second (names can change and there can be more of them, so alphabetical is not helpful here but 1 name out of all needs to come first, then all and rest ignored).
- By datetime_added (ID is auto increment so sorting by id should do the trick)
I can manage this by doing:
(SELECT * FROM scores WHERE name = "Rubio" AND option = "none" ORDER BY id DESC)
UNION
(SELECT * FROM scores WHERE name = "all" AND option = "none" ORDER BY id DESC);
This giving me necessary list of:
| id | name | datetime_added | option |
+----+-------+---------------------+--------+
| 5 | Rubio | 2023-01-01 07:05:55 | none |
+----+-------+---------------------+--------+
| 1 | all | 2023-01-01 00:10:04 | none |
+----+-------+---------------------+--------+
| 3 | all | 2023-01-01 02:08:51 | none |
+----+-------+---------------------+--------+
| 4 | all | 2023-01-01 04:13:07 | none |
+----+-------+---------------------+--------+
| 6 | all | 2023-01-01 09:17:27 | none |
+----+-------+---------------------+--------+
How could I update the table within this one query now when I know I need to:
UPDATE scores SET option = "some" WHERE id = 5;
This request is also run up to once per second, is this efficient?
2
Answers
Don’t use
UNION
, useLEFT JOIN
. Then you can useIFNULL()
to provide one selection as a default if the other selection isn’t found.DEMO
This can be done by a case statement in the order by clause ,
Use row_number window function to give update the value
Above query will update only if
name='Rubio' and
option='none'
orname ='all' and
option='none'
otherwise would do nothing because other records are filtered in the where conditionwhere
option='none' and name in ('all','Rubio')
See example