通过查询JSON列从表中选择所有记录

发布于 2025-01-30 02:02:55 字数 788 浏览 2 评论 0原文

我想通过查询以字符串格式存储JSON的特定列来选择表中的所有记录。

查询应仅通过检查包含文本的JSON字符串的值(而不是键)。

列中的JSON可能对两个不同的记录看起来如下:

{
  "title": "Test Coupon for Test Data Initializer",
  "value": "$100",
  "tag": "Test Coupon",
  "description": "Test Description",
  "expire": "10",
  "disclaimer": "NOT VALID"
}

{
  "title": "Second Record",
  "value": "$40",
  "tag": "Initializer",
  "description": "Test Description",
  "expire": "25",
  "disclaimer": "Disclaimer"
}

我想查询本列中的所有JSON的所有值,该列将匹配或包含文本并返回找到匹配的记录。

例如,如果我发送文本“初始化程序”,则应看到第一个JSON包含title中的“ initializer”,而第二个JSON将其包含在tag中。 必须返回这两个记录

因此,

SELECT * 
FROM tbl
WHERE JSON_VALUE(JSON_COLUMN, '$.*') = '%Initializer%'

I want to select all the records in a table by querying a specific column which stores a JSON in string format.

The query should match only the values (not keys) of the JSON string by checking if it contains a text.

The JSON in the column might look like the following for two different records:

{
  "title": "Test Coupon for Test Data Initializer",
  "value": "$100",
  "tag": "Test Coupon",
  "description": "Test Description",
  "expire": "10",
  "disclaimer": "NOT VALID"
}

{
  "title": "Second Record",
  "value": "$40",
  "tag": "Initializer",
  "description": "Test Description",
  "expire": "25",
  "disclaimer": "Disclaimer"
}

I want to query all the values of the JSON in this column which will match or contain a text and return the records which have found a match.

For e.g. if I send the text "Initializer", it should see that the first JSON contains "Initializer" in title and the second JSON contains it in tag. Therefore both records must be returned

I'm not too familiar with the SQL JSON functions and this is what I have so far (not working)

SELECT * 
FROM tbl
WHERE JSON_VALUE(JSON_COLUMN, '$.*') = '%Initializer%'

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

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

发布评论

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

评论(1

波浪屿的海角声 2025-02-06 02:02:55

您可以使用 OpenJSON

SELECT *
FROM t
WHERE EXISTS (
    SELECT 1
    FROM OPENJSON(t.json_column)
    WHERE value LIKE N'%initializer%'
)

You can use OPENJSON:

SELECT *
FROM t
WHERE EXISTS (
    SELECT 1
    FROM OPENJSON(t.json_column)
    WHERE value LIKE N'%initializer%'
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文