查询json字段中的数组中的指定元素
1. 问题描述?
数据存储成这样 一个json字段 我想查这个字段下scope_val
中=4950的所有数据,请问应该怎么写查询语句
{"scope": "2", "demand": "2", "manner": "1", "reduce": "1", "customer": "3", "scope_val": ["4950", "4951"], "demand_val": "200", "reduce_val": "5", "customer_val": ["3", "4", "5"]}
2. 您期望得到的结果?
要查询出截图中显示存在的3条数据
放上建表语句和示例数据
CREATE TABLE `seller_discount` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `code` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL , `title` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL , `type` tinyint NOT NULL DEFAULT '1' , `total` int NOT NULL DEFAULT '1' , `used` int NOT NULL DEFAULT '0' , `trip` int NOT NULL DEFAULT '1' , `rule` json NOT NULL , `user_id` int NOT NULL , `once_order` int NOT NULL DEFAULT '1', `overlay` int NOT NULL DEFAULT '0' , `effect_at` timestamp NULL DEFAULT NULL , `expired_at` timestamp NOT NULL , `deleted_at` timestamp NULL DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `seller_discount_code_unique` (`code`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `seller_discount` VALUES (1, 'ZUAR8TPD', NULL, 1, 10, 0, 1, '{\"scope\": \"2\", \"demand\": \"2\", \"manner\": \"1\", \"reduce\": \"1\", \"customer\": \"3\", \"scope_val\": [\"4950\", \"4951\"], \"demand_val\": \"200\", \"reduce_val\": \"5\", \"customer_val\": [\"3\", \"4\", \"5\"]}', 39, 1, 0, '2022-08-08 00:00:00', '2022-09-10 00:00:00', NULL, '2022-08-08 02:27:40', '2022-08-08 02:27:40'); INSERT INTO `seller_discount` VALUES (2, 'QV7LGR0A', '测试自动折扣', 1, 10, 0, 1, '{\"scope\": \"2\", \"demand\": \"2\", \"manner\": \"2\", \"reduce\": \"1\", \"scope_val\": [\"4950\", \"4951\"], \"demand_val\": \"200\", \"reduce_val\": \"6\"}', 39, 1, 0, '2022-08-08 00:00:00', '2022-09-10 00:00:00', NULL, '2022-08-08 02:28:49', '2022-08-08 02:28:49'); INSERT INTO `seller_discount` VALUES (3, 'HKND3C0U', '测试自动折扣2', 1, 10, 0, 1, '{\"scope\": \"2\", \"demand\": \"3\", \"manner\": \"2\", \"reduce\": \"2\", \"scope_val\": [\"4950\"], \"demand_val\": \"2\", \"reduce_val\": \"15\"}', 39, 1, 0, '2022-08-08 00:00:00', '2022-09-10 00:00:00', NULL, '2022-08-08 02:29:30', '2022-08-08 02:29:30'); INSERT INTO `seller_discount` VALUES (4, '7TIN4WDO', NULL, 2, 10, 0, 1, '{\"demand\": \"2\", \"manner\": \"1\", \"reduce\": \"1\", \"customer\": \"3\", \"demand_val\": \"200\", \"reduce_val\": \"5\", \"customer_val\": [\"3\"]}', 39, 1, 0, '2022-08-08 00:00:00', '2022-09-10 00:00:00', NULL, '2022-08-08 02:57:14', '2022-08-08 02:57:14'); INSERT INTO `seller_discount` VALUES (5, 'S6N1RM8N', '测试订单自动折扣', 2, 10, 0, 1, '{\"demand\": \"2\", \"manner\": \"2\", \"reduce\": \"2\", \"demand_val\": \"200\", \"reduce_val\": \"15\"}', 39, 1, 0, '2022-08-08 00:00:00', '2022-09-10 00:00:00', NULL, '2022-08-08 02:58:16', '2022-08-08 02:58:16'); INSERT INTO `seller_discount` VALUES (6, 'L479BE2L', NULL, 3, 10, 0, 1, '{\"demand\": \"2\", \"manner\": \"1\", \"consume\": \"2\", \"customer\": \"3\", \"demand_val\": \"200\", \"acquisition\": \"2\", \"consume_val\": [\"4950\", \"4951\"], \"customer_val\": [\"3\", \"4\", \"5\"], \"acquisition_val\": [\"4965\"]}', 39, 1, 0, '2022-08-08 00:00:00', '2022-09-10 00:00:00', NULL, '2022-08-08 07:38:09', '2022-08-08 07:38:09'); INSERT INTO `seller_discount` VALUES (7, 'ILJ8A2Q3', '测试买x送y自动折扣', 3, 10, 0, 1, '{\"demand\": \"3\", \"manner\": \"2\", \"consume\": \"2\", \"demand_val\": \"2\", \"acquisition\": \"2\", \"consume_val\": [\"4950\", \"4951\"], \"acquisition_val\": [\"4965\"]}', 39, 1, 0, '2022-08-08 00:00:00', '2022-09-10 00:00:00', NULL, '2022-08-08 07:39:18', '2022-08-08 07:39:18'); INSERT INTO `seller_discount` VALUES (8, 'DQT163OT', NULL, 4, 10, 0, 1, '{\"demand\": \"2\", \"manner\": \"1\", \"country\": \"1\", \"customer\": \"3\", \"demand_val\": \"200\", \"is_exclude\": \"1\", \"exclude_val\": \"100\", \"customer_val\": [\"3\", \"4\", \"5\"]}', 39, 1, 0, '2022-08-08 00:00:00', '2022-09-10 00:00:00', NULL, '2022-08-08 08:44:03', '2022-08-08 08:44:03');
SELECT * FROM seller_discount WHERE JSON_CONTAINS(rule->'$.scope_val',JSON_ARRAY('4950'))
参考自 blog.csdn.net/asd529735325/article...