如何根据SQLite中先前查询的结果从2个查询中选择执行

时间:2014-09-12 00:57:49

标签: sqlite

我在SQLite中有3个查询:

-query1: SELECT COUNT(id_asiento) FROM ASIENTOS_DIARIO WHERE id_paquete = 1
-query2: SELECT id_asiento FROM ASIENTOS_DIARIO
-query3: SELECT id_cta FROM CUENTASXPAQ

我想构建一个查询,我可以执行query2或query3,具体取决于query1产生的行数

这是我的代码:

SELECT CASE WHEN (SELECT COUNT(id_asiento) FROM ASIENTOS_DIARIO WHERE id_paquete = 1) > 0 
THEN (SELECT id_asiento FROM ASIENTOS_DIARIO)
ELSE (SELECT id_cta FROM CUENTASXPAQ)
END QUERY

但是当我执行它时,只返回query2或query3的第一行,我想获取所有行

怎么做

1 个答案:

答案 0 :(得分:1)

SELECT id_asiento AS ID
FROM ASIENTOS_DIARIO
WHERE (SELECT COUNT(id_asiento) FROM ASIENTOS_DIARIO WHERE id_paquete = 1) > 0 
UNION ALL
SELECT id_cta AS ID
FROM CUENTASXPAQ
WHERE (SELECT COUNT(id_asiento) FROM ASIENTOS_DIARIO WHERE id_paquete = 1) = 0