有人可以向我解释如何做吗?
Table "SC": id_sc - id_name - id_prod - qtd
Table "Name": id_name - name seller
Table "Prod": id_prod - name prod
示例:
Table Name: id 1 - name Ivan
Table Prod: id 1 - name Orange
id 2 - name Apple
id 3 - name Banana
Table SC: 2 - Ivan - Orange, Apple, Banana
我需要SC表在一个ID中包含多个产品。 我不能做表之间的关系。
答案 0 :(得分:0)
您可能没有这样的引用/映射表(这就是SC的外观)。
例如,考虑以下内容:-
DROP TABLE IF EXISTS name;
DROP TABLE IF EXISTS prod;
DROP TABLE IF EXISTS sc;
CREATE TABLE IF NOT EXISTS name (id_name INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE IF NOT EXISTS prod (id_prod INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE IF NOT EXISTS sc (id_sc INTEGER PRIMARY KEY, id_name INTEGER, id_prod INTEGER, qtd INTEGER, UNIQUE(id_name,id_prod));
INSERT INTO name (name) VALUES ('Ivan'),('Fred'),('Mary');
INSERT INTO prod (name) VALUES ('Apple'),('Banana'),('Oranage');
INSERT INTO sc (id_name,id_prod,qtd) VALUES
(1,1,10), -- Ivan has 10 apples
(2,1,9), -- Fred has 9 Apples
(3,1,21), -- Mary has 21 Apples
(1,2,33), -- Ivan has 33 Bananas
(3,2,16), -- Mary has 16 Bananas
(2,3,22), -- Fred has 22 Oranges
(3,3,5) -- Mary has 5 Oranges
;
SELECT name.name ||' has '||qtd||' '||prod.name AS who_has_what FROM sc
JOIN name ON sc.id_name = name.id_name
JOIN prod ON sc.id_prod = prod.id_prod
;
SELECT name.name, group_concat(prod.name) FROM sc
JOIN name ON sc.id_name = name.id_name
JOIN prod ON sc.id_prod = prod.id_prod
GROUP BY name.id_name
;
因此表SC将名称映射到产品。
查询的结果是:-