-- 示例表结构
CREATE TABLE products (
id INT PRIMARY KEY,
details JSON
);
-- 插入示例数据
INSERT INTO products (id, details) VALUES
(1, '{"name": "Laptop", "category": "Electronics", "tags": ["gaming", "business"]}'),
(2, '{"name": "Chair", "category": "Furniture", "tags": ["office", "home"]}'),
(3, '{"name": "Smartphone", "category": "Electronics", "tags": ["mobile", "communication"]}');
-- 使用 JSON_CONTAINS 查询包含特定标签的产品
SELECT * FROM products WHERE JSON_CONTAINS(details->'$.tags', '"gaming"');
-- 解释:
-- 1. `JSON_CONTAINS` 函数用于检查 JSON 文档中是否包含指定的值。
-- 2. `details->'$.tags'` 表示从 `details` 列中提取 `tags` 数组。
-- 3. `'"gaming"'` 是我们要查找的值,注意这里使用了双引号来表示 JSON 字符串。
-- 4. 上述查询将返回所有 `tags` 数组中包含 `"gaming"` 的记录。
上一篇:mysql字符串替换
下一篇:mysql locate函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站