在MySQL数据库中保存数组,通常有以下几种方式:
-- 创建表
CREATE TABLE example (
id INT PRIMARY KEY AUTO_INCREMENT,
data JSON
);
-- 插入数据
INSERT INTO example (data) VALUES ('["apple", "banana", "orange"]');
-- 查询数据
SELECT data FROM example WHERE id = 1;
-- 创建主表
CREATE TABLE main_table (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255)
);
-- 创建关联表
CREATE TABLE related_table (
id INT PRIMARY KEY AUTO_INCREMENT,
main_id INT,
value VARCHAR(255),
FOREIGN KEY (main_id) REFERENCES main_table(id)
);
-- 插入数据
INSERT INTO main_table (name) VALUES ('fruits');
INSERT INTO related_table (main_id, value) VALUES (1, 'apple'), (1, 'banana'), (1, 'orange');
-- 查询数据
SELECT main_table.name, related_table.value
FROM main_table
JOIN related_table ON main_table.id = related_table.main_id;
-- 创建表
CREATE TABLE example (
id INT PRIMARY KEY AUTO_INCREMENT,
data JSON
);
-- 插入数据
INSERT INTO example (data) VALUES ('["apple", "banana", "orange"]');
-- 查询数据
SELECT data->"$[1]" as fruit FROM example WHERE id = 1;
json
库)来处理。领取专属 10元无门槛券
手把手带您无忧上云