将子数据存储在单列中
Store child data in single column
我有以下表格:
CREATE TABLE titles (
id INTEGER,
title VARCHAR(255)
);
INSERT INTO titles (id, title) VALUES (1, "Mars Attacks!");
INSERT INTO titles (id, title) VALUES (2, "Da Vinci Code");
CREATE TABLE GenreName (
id INTEGER,
name VARCHAR(255)
);
INSERT INTO GenreName (id, name) VALUES (1, "Action");
INSERT INTO GenreName (id, name) VALUES (2, "Adventure");
INSERT INTO GenreName (id, name) VALUES (3, "Comedy");
INSERT INTO GenreName (id, name) VALUES (4, "Science-Fiction");
INSERT INTO GenreName (id, name) VALUES (5, "Thriller");
CREATE TABLE Genre (
title INTEGER,
genre INTEGER
);
INSERT INTO Genre (title, genre) VALUES (1, 1);
INSERT INTO Genre (title, genre) VALUES (1, 3);
INSERT INTO Genre (title, genre) VALUES (1, 4);
INSERT INTO Genre (title, genre) VALUES (2, 1);
INSERT INTO Genre (title, genre) VALUES (2, 5);
我正在寻找一种检索数据的方法
Id Title Genre
1 Mars Attacks! Action, Comedy, Science-Fiction
2 Da Vinci Code Action, Thriller
我被选择数据的递归方法困住了。
我有一个DBFiddle
使用 SQL 外部联接
select titles.id, name, title from titles left outer join genre on titles.id = genre.title left outer join genrename on genre.genre = genrename.id;
select t.id, t.title, group_concat(gn.name) from genere g join titles t on t.id = g.title 在 gn.id = g.genre 上按 t.id、t.title
加入 genrename gn
我有以下表格:
CREATE TABLE titles (
id INTEGER,
title VARCHAR(255)
);
INSERT INTO titles (id, title) VALUES (1, "Mars Attacks!");
INSERT INTO titles (id, title) VALUES (2, "Da Vinci Code");
CREATE TABLE GenreName (
id INTEGER,
name VARCHAR(255)
);
INSERT INTO GenreName (id, name) VALUES (1, "Action");
INSERT INTO GenreName (id, name) VALUES (2, "Adventure");
INSERT INTO GenreName (id, name) VALUES (3, "Comedy");
INSERT INTO GenreName (id, name) VALUES (4, "Science-Fiction");
INSERT INTO GenreName (id, name) VALUES (5, "Thriller");
CREATE TABLE Genre (
title INTEGER,
genre INTEGER
);
INSERT INTO Genre (title, genre) VALUES (1, 1);
INSERT INTO Genre (title, genre) VALUES (1, 3);
INSERT INTO Genre (title, genre) VALUES (1, 4);
INSERT INTO Genre (title, genre) VALUES (2, 1);
INSERT INTO Genre (title, genre) VALUES (2, 5);
我正在寻找一种检索数据的方法
Id Title Genre
1 Mars Attacks! Action, Comedy, Science-Fiction
2 Da Vinci Code Action, Thriller
我被选择数据的递归方法困住了。
我有一个DBFiddle
使用 SQL 外部联接
select titles.id, name, title from titles left outer join genre on titles.id = genre.title left outer join genrename on genre.genre = genrename.id;
select t.id, t.title, group_concat(gn.name) from genere g join titles t on t.id = g.title 在 gn.id = g.genre 上按 t.id、t.title
加入 genrename gn