want to know wich id of the list of IDs does not exist in the table
my_tab. I mean, the desired output should be 51, 52, 53, 54 and 55.
i assume with the current data example and number list the desired output would be
| number |
|--------|
| 3 |
| 4 |
| 51 |
| 52 |
| 53 |
| 54 |
| 55 |
This query allows gaps in both the number list and table data as you can see.
Query
SELECT
number_list.number
FROM (
SELECT
1 AS number
UNION
SELECT
2 AS number
UNION
SELECT
3 AS number
UNION
SELECT
4 AS number
# ...
# ...
UNION
SELECT
50 AS number
UNION
SELECT
51 AS number
UNION
SELECT
52 AS number
UNION
SELECT
53 AS number
UNION
SELECT
54 AS number
UNION
SELECT
55 AS number
UNION
SELECT
56 AS number
UNION
# ...
# ...
SELECT
100 AS number
) AS number_list
LEFT JOIN
Table1
ON
number_list.number = Table1.tab_id
WHERE
Table1.tab_id IS NULL
Result
| number |
|--------|
| 3 |
| 4 |
| 51 |
| 52 |
| 53 |
| 54 |
| 55 |
see demo http://sqlfiddle.com/#!9/31956e/13
Update because off comment.
Don't you think if number list goes up to 1000, it's almost impossible
to generate a list by using only Select or up to 10000 in any case
about which I am pretty sure it will.
The more dynamic query requires a MySQL number generator and nested subindex functions to work.
The number generator query which generates number 1 to 100.
So the query only works for number list up to 100 numbers.
If you need more just add a new
CROSS JOIN (
SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS record_<number>
Query
SELECT
(@row_number := @row_number + 1) AS row_number
FROM (
SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS record_1
CROSS JOIN (
SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS record_2
CROSS JOIN (SELECT @row_number := 0) AS init_user_param
see demo http://sqlfiddle.com/#!9/31956e
Use nested SUBSTRING_INDEX
functions to split items from a string in MySQL.
Query
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(
'1,2,3,4,50,51,52,53,54,55,56,100'
,',', 1
)
, ','
, -1
) AS number
see demo http://sqlfiddle.com/#!9/340e01/528
Using the two methodes and getting the results you need.
Query
SELECT
number_list.number
FROM (
SELECT
DISTINCT
SUBSTRING_INDEX(
SUBSTRING_INDEX(
'1,2,3,4,50,51,52,53,54,55,56,100' #this is the number list
,',', number_generator.row_number
)
, ','
, -1
) AS number
FROM (
SELECT
(@row_number := @row_number + 1) AS row_number
FROM (
SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS record_1
CROSS JOIN (
SELECT 1 AS n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
) AS record_2
CROSS JOIN (SELECT @row_number := 0) AS init_user_param
) AS number_generator
) AS number_list
LEFT JOIN
Table1
ON
number_list.number = Table1.tab_id
WHERE
Table1.tab_id IS NULL
see demo http://sqlfiddle.com/#!9/31956e/35