有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