SELECT ID FROM TABLE WHERE ID BETWEEN 1 AND 5
上述查询的结果将是:1,2,3,4,5
如果表格中只有id
1
和id
2
,那么它只会循环(1,2)
。
这意味着,如果id
介于1 AND 5
之间,则id
的{{1}}不存在。
3,4,5
的{{1}}是我想要选择的内容。这该怎么做?
提前致谢。
答案 0 :(得分:2)
试试这个:
select * from
(
select 1 as val
union all
select 2
union all
select 3
union all
select 4
union all
select 5
)t
left join TableName tn on t.val = tn.id
where tn.id is null
使用tally table
:
select * from (
select (3 + th*1000+h*100+t*10+u+1) x from
(select 0 th 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) A,
(select 0 h 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) B,
(select 0 t 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) C,
(select 0 u 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) D)
tn
left join t on tn.x = t.id
where tn.x between 3 and 10
and t.id is null
注意公式(3 + th*1000+h*100+t*10+u+1)
和where
第3条是srart
,10是end
。改变变量。