SQL 查询有关 OUTER JOIN 的帮助吗?

发布于 2024-09-13 20:09:57 字数 1388 浏览 5 评论 0原文

我有两张这样的桌子。

Table1

Column   |       Type       |
---------+------------------+
 cod     | text             |
 value99 | double precision |

Table2

Column   |       Type       |
---------+------------------+
 cod     | text             |
 value06 | double precision |

和我想加入它们,所以我有类似的

Column   |       Type       |
---------+------------------+
 cod     | text             |
 value99 | double precision |
 value06 | double precision |

问题是,并非所有代码都存在于两个表中,因此,如果代码不存在于其中一个表中,则其值应该为空。最后我想要这样的东西

cod      |      value99     |      value06     |
---------+------------------+------------------+
 1       |     10           |       20         |
 2       |     13           |      NULL        |
 3       |     NULL         |        15        |

我认为使用左或右连接是不可能的..或者也许是...有什么想法吗?谢谢=)

编辑: 我已经尝试过 FULL OUTER JOIN 但结果类似于

code    value  code    value
1       10     1    4
2    15     NULL    NULL
NULL NULL 3 36

ANSWER!!!: 我找到了答案,谢谢@Tobiasopdenbrouw:

SELECT test1.code,test1.value,test2.value FROM public.test1 LEFT OUTER JOIN public.test2 ON test1.code=test2.code
UNION
SELECT test2.code,test1.value,test2.value FROM public.test1 RIGHT OUTER JOIN public.test2 ON test1.code=test2.code

I have two tables like this.

Table1

Column   |       Type       |
---------+------------------+
 cod     | text             |
 value99 | double precision |

Table2

Column   |       Type       |
---------+------------------+
 cod     | text             |
 value06 | double precision |

and i'd like to join them so i'd have something like

Column   |       Type       |
---------+------------------+
 cod     | text             |
 value99 | double precision |
 value06 | double precision |

the problem is that not all the codes are present in both tables, so if a code is not present in one of the tables it's value should be null.. In the end i'd like something like this

cod      |      value99     |      value06     |
---------+------------------+------------------+
 1       |     10           |       20         |
 2       |     13           |      NULL        |
 3       |     NULL         |        15        |

I think that its not possible using LEFT or RIGHT JOIN.. or maybe it is... any ideas? Thx=)

EDITED:
I've tried the FULL OUTER JOIN but the result is something like

code    value  code    value
1       10     1    4
2    15     NULL    NULL
NULL NULL 3 36

ANSWER!!!:
i found the answer thx to @Tobiasopdenbrouw :

SELECT test1.code,test1.value,test2.value FROM public.test1 LEFT OUTER JOIN public.test2 ON test1.code=test2.code
UNION
SELECT test2.code,test1.value,test2.value FROM public.test1 RIGHT OUTER JOIN public.test2 ON test1.code=test2.code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开半夏魅人心 2024-09-20 20:09:57

我猜测了一下,因为您的问题没有详细描述所需的输出,但您可能需要的是一个辅助查询,它将为您创建一个包含所有代码的表(一个 UNION 2 个 SELECT 查询)。然后可以将该辅助表LEFT JOINED连接到您的 2 个源表)。

编辑:我自己想到了(完整)外部连接答案,但在阅读字里行间时,我认为这并不是OP真正需要的。但当然,我也可能是错的。

I'm guessing a bit, because your question doesn't describe the required output in great detail, but what you probably need is a helper query that will create a table with all the codes for you (a UNION of 2 SELECT querys). This helper table can then be LEFT JOINED to your 2 source tables).

Edit: I thought of the (FULL) OUTER JOIN answer myself, but in reading between the lines, I don't think that's what the OP really needs. But I can be wrong, of course.

森末i 2024-09-20 20:09:57

使用完全外部连接

使用完全外连接

为了保留
不匹配的信息包括
a 结果中的不匹配行
join,使用完全外连接。 SQL
服务器提供完整的外部联接
运算符 FULL OUTER JOIN,其中
包括两个表中的所有行,
不管对方是否
表有一个匹配的值。

考虑产品表的联接
以及 SalesOrderDetail 表
他们的 ProductID 列。结果
仅显示有销售的产品
对他们下达命令。 ISO全外
JOIN 运算符表示所有行
两个表中的内容都将包含在
结果,无论是否
表中有匹配的数据。

您可以将 WHERE 子句包含在
完全外连接仅返回
没有匹配数据的行
桌子之间。下列
查询仅返回那些符合以下条件的产品
也没有匹配的销售订单
作为那些不属于的销售订单
与产品相匹配(尽管所有
在这种情况下,销售订单是
与产品匹配)。

Use a FULL OUTER JOIN.

Using Full Outer Joins

To retain the
nonmatching information by including
nonmatching rows in the results of a
join, use a full outer join. SQL
Server provides the full outer join
operator, FULL OUTER JOIN, which
includes all rows from both tables,
regardless of whether or not the other
table has a matching value.

Consider a join of the Product table
and the SalesOrderDetail table on
their ProductID columns. The results
show only the Products that have sales
orders on them. The ISO FULL OUTER
JOIN operator indicates that all rows
from both tables are to be included in
the results, regardless of whether
there is matching data in the tables.

You can include a WHERE clause with a
full outer join to return only the
rows where there is no matching data
between the tables. The following
query returns only those products that
have no matching sales orders, as well
as those sales orders that are not
matched to a product (although all
sales orders, in this case, are
matched to a product).

心凉怎暖 2024-09-20 20:09:57
select 
    coalesce(t1.cod, t2.cod)
    ,t1.value99
    ,t2.value06
from 
    table1 t1 
    full outer join table2 t2 on t1.cod= t2.cod
select 
    coalesce(t1.cod, t2.cod)
    ,t1.value99
    ,t2.value06
from 
    table1 t1 
    full outer join table2 t2 on t1.cod= t2.cod
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文