在第二个表中的ID中以JSON格式嵌入ID时,我将如何加入这2个表?

发布于 2025-01-23 10:54:29 字数 562 浏览 4 评论 0原文

我想在唯一ID上加入这两个表基础,但是其中一个表中的唯一ID嵌入了JSON格式。

第一表

unique_id | product_no | product
------------------------------
345644046 |         123|  acme widget
------------------------------
476897686 |         456|  acme gadget

第二个表

supplier_id | product_info | supplier_name
------------------------------
12345787871 |[{"Id":"345644046",...... }]|  john_wick
------------------------------
72736127172 |[{"Id":"476897686",...... }]|  justin_bieber

我将如何加入这2个表,因为第二个表中的ID以JSON格式嵌入?

感谢您的帮助!

I will like to join these 2 tables base on the unique id but the unique id in one of the tables is embedded in JSON format.

1st table

unique_id | product_no | product
------------------------------
345644046 |         123|  acme widget
------------------------------
476897686 |         456|  acme gadget

2nd table

supplier_id | product_info | supplier_name
------------------------------
12345787871 |[{"Id":"345644046",...... }]|  john_wick
------------------------------
72736127172 |[{"Id":"476897686",...... }]|  justin_bieber

How will I be able to join these 2 tables as the ID in the second table is embedded in JSON format?

Thank you for your help!

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

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

发布评论

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

评论(1

并安 2025-01-30 10:54:29

Table2中的ProductInfo是产品列表吗?

在这种情况下,干净的解决方案首先将表_2分为多行,

select * from Table_2
    cross apply OPENJSON(Table_2.product_info)
         WITH ( Id int)

这将为您提供

“在此处输入图像描述”

您有很多方法可以加入您的表_1,例如:

select * from Table_1
left join (
    select * from Table_2
    cross apply OPENJSON(Table_2.product_info)
         WITH ( Id int)
     ) TmpTable on TmpTable.Id = Table_1.unique_id

还有另一种非常丑陋的方法实现这一目标,只是为了娱乐

select * from Table_1
left join Table_2 on Table_2.product_info like CONCAT('%"Id":"', Table_1.unique_id , '%')

ProductInfo in table2 is a list of product right?

In this case, the clean solution is first to split the Table_2 in multiple rows

select * from Table_2
    cross apply OPENJSON(Table_2.product_info)
         WITH ( Id int)

this will give you

enter image description here

from there you have many ways to join with your table_1, ex:

select * from Table_1
left join (
    select * from Table_2
    cross apply OPENJSON(Table_2.product_info)
         WITH ( Id int)
     ) TmpTable on TmpTable.Id = Table_1.unique_id

Also there is another very ugly way to achieve this, just for fun

select * from Table_1
left join Table_2 on Table_2.product_info like CONCAT('%"Id":"', Table_1.unique_id , '%')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文