可以在联接子句中使用 JSON 数据吗?

huangapple go评论77阅读模式
英文:

Possible to Use Json Data in a Join Clause?

问题

我有一些跟踪数据,由于一些数据在“事件”之间发生变化,所以我将其存储为SQL Server中列的JSON。

我有这个

{"items":[{"ids":[51130]}]}

现在这里有项目ID。我现在想获取“项目”的信息,但我不确定如何做到这一点,因为我需要一些连接。

如果我使用常规连接来做这个,我会有类似这样的东西

SELECT *
FROM Brands
INNER JOIN Items ON Brands.Id = Items.BrandId
INNER JOIN Events ON Events.ItemId = Items.Id
WHERE Items.Id IN (51130)

示例数据

Brands
1 Apple
2 Samsung

Items
id name description brandId
51130 Galaxy 10 "Smartphone" 2

Event
id Details
1 {"items":[{"ids":[51130]}]}

英文:

I have some tracking data and since some of the data changes between "events" I am storing it as a JSON in a column in SQL Server.

I have this

{"items":[{"ids":[51130]}

Now this has the item id. I now want to get the information of the "item" but I am not sure how to do this as I would need some joins.

If I was doing this with regular joins I would have something like this

SELECT        *
FROM            Brands INNER JOIN
                         Items ON Brands.Id = Items.BrandId
						 Events ON Events.ItemId = Items.Id
			     where Items.Id in (51130)

Sample Data

Brands
1   Apple
2   Samsung

Items
id  name description brandId
51130 Galaxy 10  "Smartphone"  2

Event
id Details 
1  {"items":[{"ids":[51130]}]

答案1

得分: 2

我希望我理解正确。您需要解析存储在 details 列中的 JSON,并将内容作为表格使用 OPENJSON() 返回。下一个示例演示了如何使用 OPENJSON() 与一个表格(请注意,问题中的 JSON 不正确):

表格:

CREATE TABLE Events (
   id int, 
   details nvarchar(max)
)

INSERT INTO Events (id, details)
VALUES (1, N'{ "items": [{ "ids": [51130] }] }')

语句:

SELECT id, itemId
FROM Events
CROSS APPLY OPENJSON(details, '$.items[0].ids') WITH (itemId int '$') 

结果:

----------
id  itemId
----------
1   51130

对于复杂的语句,您可以尝试使用适当的连接来获得预期的结果。

表格:

CREATE TABLE Brands (
   brandId int,
   brandName nvarchar(100)
)

INSERT INTO Brands
   (BrandId, BrandName)
VALUES
   (1, N'Apple'),
   (2, N'Samsung')

CREATE TABLE Items (
   id int,
   name nvarchar(50),
   description nvarchar(50),
   brandId int
)   

INSERT INTO Items
   (id, name, description, brandId)
VALUES
   (51130, N'Galaxy 10', N'Smartphone', 2)

CREATE TABLE Events (
   id int, 
   details nvarchar(max)
)

INSERT INTO Events 
   (id, details)
VALUES
   (1, N'{ "items": [{ "ids": [51130] }] }')

语句:

SELECT *
FROM Brands b
INNER JOIN Items i ON b.brandId = i.BrandId
INNER JOIN (
   SELECT id, itemId
   FROM Events
   CROSS APPLY OPENJSON(details, '$.items[0].ids') WITH (itemId int '$') 
) e ON i.id = e.itemId

结果:

----------------------------------------------------------------------
brandId brandName id     name      description brandId id    itemId
----------------------------------------------------------------------
2       Samsung   51130  Galaxy 10 Smartphone  2       1     51130
英文:

I hope I understand this correctly. You need to parse the JSON, stored in details column and return the content as a table using OPENJSON(). The next example demonstrates how to use OPENJSON() with one table (note, that the JSON in the question is not correct):

Table:

CREATE TABLE Events (
   id int, 
   details nvarchar(max)
)
INSERT INTO Events (id, details)
VALUES (1, N'{"items":[{"ids":[51130]}]')

Statement:

SELECT id, itemId
FROM Events
CROSS APPLY OPENJSON(details, '$.items[0].ids') WITH (itemId int '$') 

Result:

----------
id	itemId
----------
1	51130

For complex statements, you may try to use the appropriate joins to get the expected results.

Tables:

CREATE TABLE Brands (
   brandId int,
   brandName nvarchar(100)
)
INSERT INTO Brands
   (BrandId, BrandName)
VALUES
   (1, N'Apple'),
   (2, N'Samsung')
CREATE TABLE Items (
   id int,
   name nvarchar(50),
   description nvarchar(50),
   brandId int
)   
INSERT INTO Items
   (id, name, description, brandId)
VALUES
   (51130, N'Galaxy 10', N'Smartphone', 2)
CREATE TABLE Events (
   id int, 
   details nvarchar(max)
)
INSERT INTO Events 
   (id, details)
VALUES
   (1, N'{"items":[{"ids":[51130]}]')

Statement:

SELECT *
FROM Brands b
INNER JOIN Items i ON b.brandId = i.BrandId
INNER JOIN (
   SELECT id, itemId
   FROM Events
   CROSS APPLY OPENJSON(details, '$.items[0].ids') WITH (itemId int '$') 
) e ON i.id = e.itemId

Result:

----------------------------------------------------------------------
brandId	brandName	id	    name	    description	brandId	id	itemId
----------------------------------------------------------------------
2	    Samsung	    51130	Galaxy 10	Smartphone	2	    1	51130

答案2

得分: 0

为了开始,我假设您使用SQL的本身逻辑来插入JSON。

要检索您想要的数据,必须使用以下函数:

JSON_VALUE("yourjsoncolumn", '$.yourjson.atribute') = 51130

在这篇文章中有详细的解决方案说明。

https://learn.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver15

英文:

For begin, i suppose you use sql's own logic to insert json.

To retrieve the data you want, must use this function

JSON_VALUE("yourjsoncolumn", '$.yourjson.atribute') = 51130

In this article have full explanation to resolve your problem.

https://learn.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-ver15

huangapple
  • 本文由 发表于 2020年1月4日 01:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定