我有两张桌子: 表1:id1,field1,field2,field3 表2:id2,field1,field3
我需要的是:
"select id2 from table2 where field1 = x and field3 = y;"
如果它返回空,则执行并处理以下结果:
"select field2 from table1 where field1 = x and field3 = y;"
有没有办法在SQLite的单个查询中执行此操作?
答案 0 :(得分:1)
假设这些表可以在field1
和field3
上加入,那么这样的东西应该可以使用(警告:未经测试):
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);