我會建議三張表。第一個是Products
表,從提供產品的供應商中選擇一個名稱。第二個是Suppliers
表,其中包含有關供應商的信息。第三個是ProductSuppliers
,每個供應商和產品一行。樣本數據是:
create table Products (
ProductId int not null auto_increment primary key,
ProductName
);
create table Suppliers (
SupplierId int not null auto_increment primary key,
SupplierName
);
create table ProductSuppliers (
ProductSuppliersId int not null auto_increment primary key,
ProductId int not null references Products(ProductId),
SupplierId int not null references Suppliers(SupplierId),
SupplierProductCode varchar(255)
);
然後可以填充這些從表中:
insert into Products(ProductName)
select ProductName
from SupplierA
union
select ProductName
from SupplierB;
insert into Suppliers(SupplierName)
select 'A' union all select 'B';
insert into SupplierProducts(SupplierId, ProductId, SupplierProductCode)
select s.SupplierId, p.ProductId, a.ProductCode
from SupplierA a join
Products p
on a.ProductName = p.ProductName cross join
(select SupplierId from Suppliers where SupplierName = 'A') s;
insert into SupplierProducts(SupplierId, ProductId, SupplierProductCode)
select s.SupplierId, p.ProductId, b.ProductCode
from SupplierB b join
Products p
on b.ProductName = p.ProductName cross join
(select SupplierId from Suppliers where SupplierName = 'B') s;
使用'joins' ..... – 2014-10-05 15:55:11
你有什麼嘗試這麼遠嗎?這是一個相當直接的前瞻性SQL查詢... – ericpap 2014-10-05 15:55:22