我有2张这样的表(简化示例):
create table T (i integer, a text);
insert into T values (3,'ww');
insert into T values (1,'ee');
insert into T values (5,'nn');
insert into T values (2,'kk');
insert into T values (4,'ss');
create table U (i integer);
insert into U values (2);
insert into U values (3);
insert into U values (5);
insert into U values (1);
select rowid, i from U;
rowid i
----- --
1 2
2 3
3 5
4 1
select * from T where i in (select i from U order by rowid);
i a
----- ----------------
3 ww
5 nn
2 kk
1 ee
我需要i
中收集的in(...)
与内部选择的顺序相同,如:
select * from T where i inUnsorted (select i from U order by rowid);
i a
----- ----------------
2 kk
3 ww
5 nn
1 ee
这可能吗?
答案 0 :(得分:1)
在SQL中,除非您使用ORDER BY,否则查询是未排序的,因此外部查询需要此子句。
无论如何,您只需使用相关子查询查找值:
SELECT *
FROM T
WHERE i IN (SELECT i
FROM U)
ORDER BY (SELECT rowid
FROM U
WHERE U.i = T.i);
答案 1 :(得分:0)
这方面的东西,但更简单,即删除临时表
create temp table T2 as select i,0 as f from T;
update T2 set f=(select rowid from U where U.i=T2.i);
select * from T2;
i f
----- ----------------
3 2
1 4
5 3
2 1
4
select T.i,a from T join T2 on T.i=T2.i where T2.f order by T2.f;
i a
----- ----------------
2 kk
3 ww
5 nn
1 ee
答案 2 :(得分:0)
我对这个问题感到困惑。但如果我猜对了,这应该是你想要的?
select T.*, U.i as ui from T inner join U on T.i = U.i;