如果其中一个表在要连接的列中具有重复值,是否可以连接两个表?
假设我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以进行任何您想要的连接,甚至是“自连接”,但如果关键字之间没有重合或匹配,您的查询可能为空。如果你想列出全部,你应该使用 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.