【问题标题】:mysql json where clausemysql json where 子句
【发布时间】:2019-10-04 21:24:34
【问题描述】:

我在 PRICING_DATA 表中有一个包含以下 json 数据类型列的表

pricingJson type json nullable

我正在使用 sql 来查询表。

select * from `PRICING_DATA` where `pricingJson`->"$.product.productFamily" = "Compute Instance";

示例 json 数据如下所示

{
"product": {
    "productFamily": "Compute Instance",
    "attributes": {
        "enhancedNetworkingSupported": "Yes",.....

但是查询没有返回任何行。 我在这里做错了什么?

数据库中的 JSON 原始字符串似乎已转义。

"{\"product\":{\"productFamily\":\"Compute Instance\",\"attributes\":{\"enhancedNetworkingSupported\":\"Yes

我使用了下面的 json unquote,但它仍然没有给我任何行。

select * from `PRICING_DATA` where JSON_UNQUOTE(JSON_EXTRACT(pricingJson, "$.product.productFamily")) = "Compute Instance";

【问题讨论】:

  • 为我工作:dbfiddle.uk/… 也许你的价值观中有一些奇怪的字符?
  • @Nick :你是对的,这确实是那些反斜杠字符。我将更新帖子以显示修复。

标签: mysql json sqldatatypes mysql-8.0


【解决方案1】:

您需要“取消引用” JSON 字符串才能对其进行比较。

select * from `PRICING_DATA` where `pricingJson`->>"$.product.productFamily" = "Compute Instance";

文档:https://dev.mysql.com/doc/refman/8.0/en/json-search-functions.html#operator_json-inline-path


使用pricingJson->"$.product.productFamily" 是简写

JSON_EXTRACT(pricingJson, "$.product.productFamily")

返回值,但作为 引用 字符串。所以:

SELECT pricingJson->"$.product.productFamily" FROM PRICING_DATA

会返回:

+-----------------------------------------+
| pricingJson->"$.product.productFamily"  |
+-----------------------------------------+
| "Compute Instance"                      |
+-----------------------------------------+

您需要使用JSON_UNQUOTE() 函数删除引号,而使用pricingJson->>"$.product.productFamily" 是以下的简写:

JSON_UNQUOTE(JSON_EXTRACT(pricingJson, "$.product.productFamily"))

【讨论】:

  • 是什么让你这么想? OPs 代码在我的演示中运行良好 dbfiddle.uk/…
  • 我认为有一个转义的 json 问题。在 DB 中,我看到 Json 条目被转义了。
猜你喜欢
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-13
  • 2010-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多