如何使用 INNER JOIN 将行添加到 SQL 查询的结果中?
在过去,当我需要向 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
How can I add my extra row to my_table when it is INNER像这样加入到另一张桌子吗?
更新:哇,我刚才搞砸了。快到回家的时间了。我忘记了 where 子句!
SELECT t1.colA, t1.colB, t2.colC
FROM my_table t1
INNER JOIN my_other_table t2
ON t1.colB = t2.colC
In times past, when I need to add a row to the result of a SQL statement, I write a statement like this:
SELECT colA, colB FROM my_table
UNION
SELECT 'foo' AS colA, 'bar' as colB;
However, suppose I've written the following SQL:
SELECT t1.colA, t1.colB, t2.colC FROM my_table t1 INNER JOIN my_other_table t2
How can I add my extra row to my_table when it is INNER JOINed to another table like this?
Update: Wow, I just goofed. It's almost going-home time. I forgot my where clause!
SELECT t1.colA, t1.colB, t2.colC
FROM my_table t1
INNER JOIN my_other_table t2
ON t1.colB = t2.colC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需将第二个查询中的
mytable
替换为(SELECT ...)
,整个第一个查询(在括号中):Just replace
mytable
in your second query with(SELECT ...)
, the whole first query (in parenthesis):