Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

mysql json_contains

作者:寂寞带点伤   发布日期:2025-04-18   浏览:102

-- 示例表结构
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函数

大家都在看

mysqlavg函数保留小数

mysql经纬度距离计算

存储过程mysql

mysql with语句

mysql时间加减

brew 启动mysql

unsigned在mysql中是什么意思

mysql 插入更新

mysql.user

python mysql update

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站