MySQL查询表中不存在的id
要查询的id数组:[ 0, 1, 2, 3, 4, 5, 10000000, 10000001 ]
表中存在的id值:1, 2, 3, 4, 5
查询结果:
SQL:
SELECT
t3.id
FROM
(
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(ids, ',', ht.help_topic_id), ',', -1) AS id
FROM
(
SELECT
REPLACE(REPLACE(REPLACE(ids, ' ', ''), '[', ''), ']', '') AS ids
FROM
(SELECT '[ 0, 1, 2, 3, 4, 5 ]' AS ids) AS t1
) AS t2
INNER JOIN mysql.help_topic AS ht
WHERE
ht.help_topic_id BETWEEN 1 AND LENGTH(ids) - LENGTH(REPLACE(ids, ',', '')) + 1
) AS t3
LEFT JOIN user AS u ON t3.id = u.id
WHERE u.id IS NULL;