我在SQLite3数据库中有这样的表(在任何关系中都是白色的):
TAG_NUMBER INT ITEM_NAME VARCHAR
每个标签号都是唯一的(主键)。 例如:
TAG_NUMBER ITEM_NAME
1001 N2
1002 NY
1003 NR
1005 YT
1008 TE
我想选择所有缺席的标签号码: 例如:
Result-->
TAG_NUMBER
1004
1006
1007
答案 0 :(得分:1)
这需要SQLite 3.8.3或更高版本:
WITH RECURSIVE all_numbers(n)
AS (SELECT MIN(tag_number) FROM MyTable
UNION ALL
SELECT n + 1 FROM all_numbers
WHERE n < (SELECT MAX(tag_number) FROM MyTable))
SELECT n FROM all_numbers
WHERE n NOT IN (SELECT tag_number FROM MyTable)