Oracle 查询的时候 我需要造出数据来 比如select *from table_name where code in(01,02,03) 然后我要返回的数据是01,1 (换行)01,2(换行) 02,1 .........
问题描述:
现在需要查询的时候关联一个子查询的表 查询sql如下
假设table里面code有01,02,03
select *from table_name where code in(01,02,03)
然后结果得是
01 1
01 2
01 3
02 1
02 2
02 3
03 1
03 2
03 3
请问这个sql怎么写呢,跪谢
答
这个简单,创建一个表test_num,加入三条记录1,2,3
CREATE TABLE `test_num` (
`id` INT(11) NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `test_num` (`index`) VALUES (1);
INSERT INTO `test_num` (`index`) VALUES (2);
INSERT INTO `test_num` (`index`) VALUES (3);
然后交叉关联你的表查询就好了
select t.code, n.id from table_name t, test_num n where t.code in(01,02,03)
答
你是表中没有1、2、3只有01、02、03然后想查询的结果为
01 1
01 2
01 3
...
这样的结果?
答
没太看明白你的意思?
答
你是想加上序号把,例如01的数据有3个,序号分别为1,2,3?
答
with xx as
(select 1 a from dual
union
select 2 a from dual
union
select 4 a from dual
)
,yy as(
select count(1) as cn from xx)
select distinct a,level
from xx,yy
connect by level <=cn
order by level,a