我在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的第一行,我想获取所有行
怎么做
答案 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