Sql Oracle联合取决于IF条件
我必须对所有这些语句进行SQL查询: 首先,我有第一个SQL查询,如下所示:Sql Oracle联合取决于IF条件,sql,oracle,Sql,Oracle,我必须对所有这些语句进行SQL查询: 首先,我有第一个SQL查询,如下所示: SELECT COUNT(*) as NbOfEntries from table1 where colName = "some condition" SELECT t2.col1 as col1, t2.col2 as col2, t3.col3 as col3 FROM table2 t2 INNER JOIN table3 t3 ON t2.colName = t3.colNam
SELECT COUNT(*) as NbOfEntries from table1 where colName = "some condition"
SELECT
t2.col1 as col1,
t2.col2 as col2,
t3.col3 as col3
FROM table2 t2
INNER JOIN table3 t3
ON t2.colName = t3.colName
WHERE t2.colName = "some condition"
SELECT
"Dumb string 1" as col1,
"Dumb string 2" as col2,
"Dumb string 3" as col3
FROM DUAL -- or something like that, I don't know how it works
col1 col2 col3
A AB 3
A AC 7
A AD 2
X XX 0 // << Add this last row
然后,我有另一个SQL语句,如下所示:
SELECT COUNT(*) as NbOfEntries from table1 where colName = "some condition"
SELECT
t2.col1 as col1,
t2.col2 as col2,
t3.col3 as col3
FROM table2 t2
INNER JOIN table3 t3
ON t2.colName = t3.colName
WHERE t2.colName = "some condition"
SELECT
"Dumb string 1" as col1,
"Dumb string 2" as col2,
"Dumb string 3" as col3
FROM DUAL -- or something like that, I don't know how it works
col1 col2 col3
A AB 3
A AC 7
A AD 2
X XX 0 // << Add this last row
最后,基于第一条SQL语句,如果NbOfEntries>0
,我必须在第二条语句的结果上添加一行伪数据。我必须向第二个SQL查询添加一个UNION
,如下所示:
SELECT COUNT(*) as NbOfEntries from table1 where colName = "some condition"
SELECT
t2.col1 as col1,
t2.col2 as col2,
t3.col3 as col3
FROM table2 t2
INNER JOIN table3 t3
ON t2.colName = t3.colName
WHERE t2.colName = "some condition"
SELECT
"Dumb string 1" as col1,
"Dumb string 2" as col2,
"Dumb string 3" as col3
FROM DUAL -- or something like that, I don't know how it works
col1 col2 col3
A AB 3
A AC 7
A AD 2
X XX 0 // << Add this last row
我曾想过使用UNION
,但我不知道如何在有条件的情况下使用UNION
。此外,我不知道如何在Oracle中使用SELECT
生成假数据,我已经了解了DUAL
的工作原理,但我还没有真正能够向前推进
多谢各位
编辑:样本数据
如果第一条语句返回0,则有以下行:
col1 col2 col3
A AB 3
A AC 7
A AD 2
如果第一条语句返回>0,我必须向结果中添加一个伪行,如下所示:
SELECT COUNT(*) as NbOfEntries from table1 where colName = "some condition"
SELECT
t2.col1 as col1,
t2.col2 as col2,
t3.col3 as col3
FROM table2 t2
INNER JOIN table3 t3
ON t2.colName = t3.colName
WHERE t2.colName = "some condition"
SELECT
"Dumb string 1" as col1,
"Dumb string 2" as col2,
"Dumb string 3" as col3
FROM DUAL -- or something like that, I don't know how it works
col1 col2 col3
A AB 3
A AC 7
A AD 2
X XX 0 // << Add this last row
col1 col2 col3
A AB 3
A AC 7
A广告2
X XX 0/您可以这样使用EXISTS
:
SELECT
t2.col1 as col1,
t2.col2 as col2,
t3.col3 as col3
FROM table2 t2
INNER JOIN table3 t3
ON t2.colName = t3.colName
WHERE t2.colName = "some condition"
UNION ALL
SELECT
"Dumb string 1" as col1,
"Dumb string 2" as col2,
"Dumb string 3" as col3
FROM DUAL
WHERE EXISTS (
SELECT 1 from table1
where colName = "some condition"
)
无需对表1
的行进行计数
EXISTS
如果有一行满足条件,将返回TRUE
。您可以使用HAVING
子句进行筛选:
SELECT t2.col1 as col1, t2.col2 as col2, t3.col3 as col3
FROM table2 t2
INNER JOIN table3 t3 ON t2.colName = t3.colName
WHERE t2.colName = 'some condition'
UNION ALL
SELECT 'X', 'XX', 0
FROM table1
WHERE colName = 'some condition'
HAVING COUNT(*) > 1
请向我们展示样本数据和预期结果,以澄清您的问题。@GMB我已更新了问题