I have two tables: Table1: id1, field1, field2, field3 Table2: id2, field1, field3
What I need is:
"select id2 from table2 where field1 = x and field3 = y;"
if it r开发者_如何学运维eturns empty then perform and process the results from:
"select field2 from table1 where field1 = x and field3 = y;"
Is there a way to perform this in a single query in SQLite?
Assuming that the tables are joinable on field1
and field3
, then something like this should work (Warning: Untested):
select
case when sum(count_id2)>0 then a.id2
else b.field2 end as column
from(
select id2,count(1) as count_id2
from table2
where field1=x and field3=y
group by id2
)a
join(
select id2,field2
from table1
join table2
using(field1,field3)
where field1=x and field3=y
)b
using(id2);
精彩评论