sql联合查询与sqlite中的复杂子查询

时间:2014-01-08 13:31:09

标签: sql sqlite

我需要对两个复杂子查询的结果进行联合。我有以下查询在sqlite

中工作
select * from (select 
               objectid from osm where tag = 'b' 
                     union 
               select 
               objectid from osm where tag = 'a');

我想将其修改为如下(简化):

select * from (select objectid from osm where tag = 'b' limit 10 
               union 
               select objectid from osm where tag = 'a' limit 10);

在sqlite中无效。所以我试图做的是

select * from 
    (select objectid from osm where tag = 'b' limit 10) as b, 
    (select objectid from osm where tag = 'a' limit 10) as a, 
    osm
where 
    osm.objectid in (a union b);

但这又是无效的。我知道我能做到

 osm.objectid in a OR osm.objectid in b;

但由于某些技术原因,我无法使用OR。没有OR会有什么办法吗?

2 个答案:

答案 0 :(得分:1)

LIMIT适用于整个查询,因此您必须使用单独的子查询层才能进行UNION LIMITed查询:

select * from
  (select * from (select objectid from osm where tag = 'b' limit 10)
   union 
   select * from (select objectid from osm where tag = 'a' limit 10));

答案 1 :(得分:0)

这是一个我认为可以让你做你想做的伎俩:

select *
from osm
where objectid in (select objectid from osm where tab = 'b' limit 10) or
      objectid in (select objectid from osm where tab = 'b' limit 10);