是的,MySQL 从 5.7.8 版本开始原生支持 JSON 数据类型,提供了高效的 JSON 数据存储和查询功能。
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
attributes JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO products (name, attributes)
VALUES ('Laptop', '{"color": "silver", "memory": "16GB", "storage": "512GB SSD"}');
-- 提取特定属性
SELECT name, attributes->"$.color" AS color FROM products;
-- 使用 JSON 路径表达式
SELECT JSON_EXTRACT(attributes, '$.memory') AS memory FROM products;
MySQL 的 JSON 支持使得在关系型数据库中存储和查询半结构化数据变得更加方便。