-- 创建一个包含 JSON 列的表
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
info JSON
);
-- 为 JSON 列中的特定字段创建索引
-- 假设我们想为 info 中的 "price" 字段创建索引
CREATE INDEX idx_price ON products ((CAST(info->>'$.price' AS DECIMAL(10,2))));
-- 插入一些示例数据
INSERT INTO products (info) VALUES
('{"name": "Product A", "price": 10.50}'),
('{"name": "Product B", "price": 20.75}'),
('{"name": "Product C", "price": 5.25}');
-- 使用索引进行查询
SELECT * FROM products WHERE CAST(info->>'$.price' AS DECIMAL(10,2)) > 10;
info 的表 products。price)创建索引。这里使用了 CAST 和 ->> 操作符来提取并转换 JSON 字段的值,以便可以对其进行索引。上一篇:mysql source 执行
下一篇:mysql date转字符串
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站