可以使用以下SQL語(yǔ)句來(lái)查詢(xún)序號(hào)中未出現(xiàn)的數(shù)據(jù):
SELECT missing_numbers.number
FROM (
SELECT ones.number + tens.number * 10 + hundreds.number * 100 AS number
FROM (SELECT 0 AS number UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS ones
CROSS JOIN (SELECT 0 AS number UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS tens
CROSS JOIN (SELECT 0 AS number UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) AS hundreds
) AS missing_numbers
LEFT JOIN your_table ON missing_numbers.number = your_table.id
WHERE your_table.id IS NULL
ORDER BY missing_numbers.number;
在這個(gè)SQL語(yǔ)句中,我們首先生成一個(gè)包含所有可能序號(hào)的臨時(shí)表missing_numbers,然后左連接your_table表,通過(guò)判斷your_table.id是否為NULL來(lái)確定序號(hào)中是否未出現(xiàn)的數(shù)據(jù)。最后按照序號(hào)進(jìn)行排序輸出。