gpt4 book ai didi

mysql - 选择中间不存在的ID

转载 作者:可可西里 更新时间:2023-11-01 08:29:56 25 4
gpt4 key购买 nike

SELECT ID FROM TABLE WHERE ID BETWEEN 1 AND 5

上面查询的结果将是:1,2,3,4,5

如果只有 id 1id 2谁存在于表中,那么它将循环(1,2)只要。

这意味着,如果 id介于 1 AND 5 之间, id3,4,5不存在。

id3,4,5是我要选择的。这该怎么做?

提前致谢。

最佳答案

试试这个:

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

使用计数表:

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 clause 3 is srart, 10 是 end。改变变量。

这是 fiddle :http://sqlfiddle.com/#!9/2f53f/2

关于mysql - 选择中间不存在的ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com