有Oracle中的表中的列之间:SQL查询返回行,其中编号的列表是开始和结束值
id | start_number | end_number
---+--------------+------------
1 | 100 | 200
2 | 151 | 200
3 | 25 | 49
4 | 98 | 99
5 | 49 | 100
有数字(50,99,150)的列表。
我想要一个sql语句,它返回数字列表中的任何数字与start_number和end_number之间相等的所有id。
使用上面的例子;应该返回1,4和5。
1 - 150是和之间或等于100 200
2 - 没有数字的是之间或等于151和200
3 - 没有数字的是之间或等于25和49
4 - 99在和之间或等于98 99
5 - 50 99之间或等于49和100
drop table TEMP_TABLE;
create table TEMP_TABLE(
THE_ID number,
THE_START number,
THE_END number
);
insert into TEMP_TABLE(THE_ID, THE_START, THE_END) values (1, 100, 200);
insert into TEMP_TABLE(THE_ID, THE_START, THE_END) values (2, 151, 200);
insert into TEMP_TABLE(THE_ID, THE_START, THE_END) values (3, 25, 49);
insert into TEMP_TABLE(THE_ID, THE_START, THE_END) values (4, 98, 99);
insert into TEMP_TABLE(the_id, the_start, the_end) values (5, 49, 100);
以下是我想出了根据该意见,并回答下面加上一些额外的研究解决方案:
SELECT
*
from
TEMP_TABLE
where
EXISTS (select * from(
select column_value as id
from table(SYS.DBMS_DEBUG_VC2COLL(50,99,150))
)
where id
BETWEEN TEMP_TABLE.the_start AND TEMP_TABLE.the_end
)
这工作太:
SELECT
*
from
TEMP_TABLE
where
EXISTS (select * from(
select column_value as id
from table(sys.ku$_vcnt(50,99,150))
)
where id
BETWEEN TEMP_TABLE.the_start AND TEMP_TABLE.the_end
)
谢谢你的快速反应(和民主党重新格式化我的问题)。 – irrational 2012-01-05 18:17:08
在这种情况下,数字列表不在表格中。有没有办法做到这一点,而不把数字列表放入不同的表格? – irrational 2012-01-05 18:18:07
你如何提供数字列表? – MatBailie 2012-01-05 18:19:22