有两张表,表between (表1)num1 num2
1 5
12 18
22 25
........
........表phone (表1)id tel
2 2476443
3 8476443
7 8990034
13 8923456
19 4545456
25 4546565
.........
.........我自己的写法是:select * from phone where (id>=1 and id<=5) or (id>=12 and id<=18) (id>=22 and id<=25)
输出id tel
2 2476443
3 8476443
13 8923456
25 4546565我这个做法明显不科学,而且表between 如果有几白条记录,难道我也手工做吗?有什么好的办法实现一样的效果吗?
请高手指点?
1 5
12 18
22 25
........
........表phone (表1)id tel
2 2476443
3 8476443
7 8990034
13 8923456
19 4545456
25 4546565
.........
.........我自己的写法是:select * from phone where (id>=1 and id<=5) or (id>=12 and id<=18) (id>=22 and id<=25)
输出id tel
2 2476443
3 8476443
13 8923456
25 4546565我这个做法明显不科学,而且表between 如果有几白条记录,难道我也手工做吗?有什么好的办法实现一样的效果吗?
请高手指点?
WHERE EXISTS(SELECT 1 FROM [between] b where a.id between b.num1 and b.num2)这样不一定好
(select num1, num2 FROM 表between) a
CROSS APPLY
(SELECT * FROM 表phone WHERE id BETWEEN a.num1 AND a.num2) b
--表phone id字段最好有聚集索引,最起码也要有索引