MySQL - 搜索JSON类型行以查找打开的商店

时间:2018-06-12 11:34:32

标签: mysql

我有一个以 JSON 格式存储工作时间的表格。我正在尝试找到选择查询时打开的所有商店的方法。

这是存储小时数的示例(存储在数据库中,未格式化):

{"friday-to": "18:00", "monday-to": "18:00", "sunday-to": null, "friday-to2": "06:00", "monday-to2": "06:00", "sunday-to2": null, "tuesday-to": "18:00", "friday-from": "07:00", "monday-from": "07:00", "saturday-to": "15:00", "sunday-from": null, "thursday-to": "18:00", "tuesday-to2": "06:00", "friday-from2": "06:00", "monday-from2": "06:00", "saturday-to2": "06:00", "sunday-from2": null, "thursday-to2": "06:00", "tuesday-from": "07:00", "wednesday-to": "18:00", "saturday-from": "09:00", "thursday-from": "07:00", "tuesday-from2": "06:00", "wednesday-to2": "06:00", "saturday-from2": "06:00", "thursday-from2": "06:00", "wednesday-from": "07:00", "wednesday-from2": "06:00"}

为了更好的观点而形成:

{
    "friday-to": "18:00",
    "monday-to": "18:00",
    "sunday-to": null, 
    "friday-to2": "06:00", 
    "monday-to2": "06:00",
    "sunday-to2": null, 
    "tuesday-to": "18:00", 
    "friday-from": "07:00", 
    "monday-from": "07:00", 
    "saturday-to": "15:00", 
    "sunday-from": null, 
    "thursday-to": "18:00", 
    "tuesday-to2": "06:00", 
    "friday-from2": "06:00", 
    "monday-from2": "06:00", 
    "saturday-to2": "06:00", 
    "sunday-from2": null, 
    "thursday-to2": "06:00", 
    "tuesday-from": "07:00", 
    "wednesday-to": "18:00", 
    "saturday-from": "09:00", 
    "thursday-from": "07:00", 
    "tuesday-from2": "06:00", 
    "wednesday-to2": "06:00", 
    "saturday-from2": "06:00", 
    "thursday-from2": "06:00", 
    "wednesday-from": "07:00", 
    "wednesday-from2": "06:00"
}

目前只有第一个 -to 处于有效状态, -to2 仍然无效。

例如,我想查找今天(当前星期二)开业的所有商店。有可能这样做吗?

当前的MySQL版本:

mysql  Ver 14.14 Distrib 5.7.22

1 个答案:

答案 0 :(得分:1)

MySQL manual包含可能有用的函数JSON_EXTRACT的示例。它表明你可以按照以下方式做点什么:

SELECT c, JSON_EXTRACT(c, "$.id"), g
FROM jemp
WHERE JSON_EXTRACT(c, "$.id") > 1
ORDER BY JSON_EXTRACT(c, "$.name");

所以你可以这样做:

SELECT * FROM my_table 
WHERE (
    JSON_EXTRACT(field, "$.tuesday-from") IS NOT NULL
    OR JSON_EXTRACT(field, "$.tuesday-from2") IS NOT NULL
    )