mysql 数据库中存放json数据查询方式-JSON_CONTAINS用法

mysql | 2021-12-28 15:19:15 | 阅读 143 次 | 评论(0)

创建测试表

CREATE TABLE `sys_log` (

  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message_type` varchar(255) DEFAULT NULL,
  `message_body` text,
  PRIMARY KEY (`id`) USING BTREE

) ENGINE=InnoDB AUTO_INCREMENT=5412 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

补充测试数据

INSERT INTO `sys_log`(`id`,`message_type`,`message_body`) VALUES (1,"上线","{"online":"1"}"),(2,"下线","{"online":"0"}"),(3,"告警1",'[{"name":"WATER_DETC","serialId":"0","value":"1"}]'),(4,"告警2",'[{"name":"WATER_DETC","serialId":"0","value":"0"}]'),(5,"告警3",'[{"name":"DEMOLISH","serialId":"0","value":"1"}]')

查询sql

select * from sys_log where message_body ->"$.online" = "1" or JSON_CONTAINS(message_body->"$[*].name", '"WATER_DETC"', "$")


文章评论,共0条
游客请输入验证码
最新评论