mysql的JSON字段查询

jyone 2017-10-08 01:57:57
订单表中room字段中有一条记录是这样子:

[{"room_id": 22, "roomName": "306号房间"}, {"room_id": 24, "roomName": "308号房间"}]

请问如何查找包含roomName为306的记录呢?
我这样写不行select * from bill where room->'$
  • .roomName' = "306" ;

    求正确的写法。
...全文
963 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2017-10-09
  • 打赏
  • 举报
回复
room->'$
  • .roomName'  返回的是一个 JSON 数组,所以外面再套一层 JSON_CONTAINS 才行 where JSON_CONTAINS
  • ( room->'$
  • .roomName' , '"306号房间"', '$'
  • )
ACMAIN_CHM 2017-10-08
  • 打赏
  • 举报
回复
JSON_CONTAINS(json_doc, val[, path]) Returns 0 or 1 to indicate whether a specific value is contained in a target JSON document, or, if a path argument is given, at a specific path within the target document. Returns NULL if any argument is NULL or the path argument does not identify a section of the target document. An error occurs if either document argument is not a valid JSON
jyone 2017-10-08
  • 打赏
  • 举报
回复
这个是示例哦,这样子写取不到数据 select * from bill where room->'$
  • .roomName' = "306号房间" ;

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧