过去,当我需要在SQL语句的结果中添加一行时,我会写一个这样的语句:
SELECT colA, colB FROM my_table
UNION
SELECT 'foo' AS colA, 'bar' as colB;
但是,假设我编写了以下SQL:
SELECT t1.colA, t1.colB, t2.colC FROM my_table t1 INNER JOIN my_other_table t2
当INNER JOIN到另一个像这样的表时,如何将我的额外行添加到my_table?
更新:哇,我只是傻逼。这几乎是回家的时间。我忘记了我的where子句!
SELECT t1.colA, t1.colB, t2.colC
FROM my_table t1
INNER JOIN my_other_table t2
ON t1.colB = t2.colC
答案 0 :(得分:1)
SELECT t1.colA, t1.colB, t2.colC FROM my_table t1 INNER JOIN my_other_table t2
UNION
SELECT 'foo' as colA, 'bar' as colB, 'baz' as colC
答案 1 :(得分:1)
SELECT
(t1.colA, t1.colB FROM my_table
UNION
SELECT 'foo' AS colA, 'bar' as colB) as t1
INNER JOIN
my_other_table t2 ON . . .
答案 2 :(得分:0)
只需将第二个查询中的mytable
替换为(SELECT ...)
,即第一个查询(括号内):
SELECT t1.colA, t1.colB, t2.colC
FROM
( SELECT colA, colB
FROM my_table
UNION
SELECT 'foo' AS colA, 'bar' as colB
) AS t1
INNER JOIN my_other_table t2
ON t1.colB = t2.colC
;