I have a table that contains IDs.
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 6 |
| 9 |
| 7 |
| 10 |
Number 5 and number 8 are not in the table.
I want to select only those rows from the table
I have a table that contains IDs.
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 6 |
| 9 |
| 7 |
| 10 |
Number 5 and number 8 are not in the table.
I want to select only those rows from the table
3
Answers
If you want the missing
id
s, one option is a recursive query to generate the number list, and thennot exists
(this requires MySQL 8.0):You can get the gaps by looking at the previous row. If your table is not too large:
Actually splitting this out into separate rows is tricky in earlier versions of MySQL unless you have a number or tally table.
If your data is large, you can use variables instead:
fiddle
If there is a lot of consecutive lost numbers only the last one will be returned.