如果其中一个表在要连接的列中具有重复值,是否可以连接两个表?

发布于 2025-01-08 04:37:08 字数 831 浏览 0 评论 0原文

假设我将 Shoes 表连接到 Clothes 表,它们都有一个名为 ShoesID 的列,所以对我来说这是有意义的在 ShoesID 上连接这 2 个表(它也恰好是 Shoes 表的主键)。但这是我的问题,它不是 Clothes 表的主键,因此在 Clothes 表中,在 ShoesID 列中,一些行重复出现,这破坏了我的连接。

有办法解决这个问题吗?

Clothes Table
ClothesID   ShoesID   NakedVarchar
99      |1           |  e|
100     |1           |  f|
101     |4           |  g|
102     |4           |  d|

I want to join this to this:



Shoes Table

ShoesID  Descriptionvarchar
|1        |  a|
|2        |  b|
|3        |  c|
|4        |  d|




所以我认为这样做的逻辑方法是

LEFT JOIN Clothes ON Shoes.ShoesID = Clothes.ShoesID

不幸的是,因为 Clothes 表包含重复项,所以 Postgres 似乎将它们删除了, 我希望连接所有数据,包括重复数据,我该如何解决这个问题?

这并不像反转我的连接语句那么简单,因为我在技术上试图将它们加入一个大的 有许多其他连接的查询。

let's say I'm Joining the Shoes table to the Clothes table, They both have a column called ShoesID, so to me it would make sense to join those 2 tables on ShoesID (it also happens to be the primary key of the Shoes table). But here's my problem, it's not the primary key of the Clothes table, so in the Clothes table, in the ShoesID column, some of the rows repeat themselves and that's ruining my join.

Is there a way to get around that?

Clothes Table
ClothesID   ShoesID   NakedVarchar
99      |1           |  e|
100     |1           |  f|
101     |4           |  g|
102     |4           |  d|

I want to join this to this:



Shoes Table

ShoesID  Descriptionvarchar
|1        |  a|
|2        |  b|
|3        |  c|
|4        |  d|




so I figured the logical way of doing this would be to do

LEFT JOIN Clothes ON Shoes.ShoesID = Clothes.ShoesID

unfortunately because the Clothes table contains duplicates it seems Postgres cuts them out,
I'd like all the data to be joined including the duplicates, how can I get around this?

it's not as simple as reversing my join statement as I'm technically trying to join them in a big
query that has got many other joins.

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

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

发布评论

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

评论(1

你在看孤独的风景 2025-01-15 04:37:08

您可以进行任何您想要的连接,甚至是“自连接”,但如果关键字之间没有重合或匹配,您的查询可能为空。如果你想列出全部,你应该使用 UNION 而不是 join。

you can make any join that you want to, even a "self-join", but if there is no coincidence or match between the keywords your query could be empty. if you want to list them all, you should use a UNION instead of a join.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文