如何在MySQL中搜索json数组值的数据

时间:2017-12-31 04:36:44

标签: mysql sql json

我从未使用json data在我的数据库中存储任何值,现在情况促使我使用它。

以下是我的json格式,我将其存储到MySQL数据库

{
    "wrestling_board": [{
        "Price": "400",
        "Quantity": "1",
        "Amount": "400"
    }],
    "sign_board": [{
        "Price": "200",
        "Quantity": "1",
        "Amount": "200"
    }],
    "total": [{
        "Price": null,
        "Quantity": null,
        "Amount": "600"   <-- total price
    }]
}

Here is MySQL Demo:

如何搜索所有results where total.amount > 3000

1 个答案:

答案 0 :(得分:1)

更新: 如果您的字段是json,则可以使用如下查询:

REGEXP

MySQL Fiddle Demo

我认为一种方法是使用select customer_name,company_name,email,quotation_data from sent_quotation_data where quotation_data REGEXP '"total":.+"Amount":"(3[0-9][0-9][1-9]|[4-9][0-9]{3}|[1-9][0-9]{4,})"'; 这样:

"total":                     --> Finding total between " then :
.+                           --> Some characters
"Amount":"                   --> Finding Amount between " then : then "
(                            --> start a combination
    3[0-9][0-9][1-9]         --> accepting range of 3001-3999
    |                        --> or
    [4-9][0-9]{3}            --> accepting range of 4000-9999
    |                        --> or
    [1-9][0-9]{4,}           --> accepting range of 10000-...
)                            --> end of combination
"                            --> following by "

MySQL Fiddle Demo

npm install -g vue-cli