1、创建表:
sql
DROP TABLE IF EXISTS `client`;
CREATE TABLE `client` (
`id` int NOT NULL AUTO_INCREMENT,
`client_ids` json NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of client
-- ----------------------------
INSERT INTO `client` VALUES (1, '[\"a\", \"b\"]');
INSERT INTO `client` VALUES (2, '[\"a\", \"ab\"]');
2、查询json数组中含有"b"元素的记录:("ab" 不算 "b")
sql
select * from client t where JSON_CONTAINS(t.client_ids, JSON_ARRAY('b'), '$');
执行结果: