我试图找到一种简单的方法来从3个表中选择结果并从每个选择中获取返回的列并将它们全部插入到第4个表中,这将作为基于“可用性”的“查找矩阵”。 typeId,manufacturerId和locationId。
因此,例如,如果有10个TYPEID和5个MANUFACTURERID并说3个LOCATIONID,我会得到10x5x3,因此将150行插入TABLE4
表1,2和表之间没有直接联系。 3,它们只是产品类型,制造商和位置的独立表。 稍后,TABLE4中将有一个“标志”列,用于确定哪个可用性为真/假
这样的东西,但我不知道语法:
SELECT DISTINCT TYPEID FROM TABLE1
Loop
SELECT DISTINCT MANUFACTURERID FROM TABLE2
Loop
SELECT DISTINCT LOCATIONID FROM TABLE3
Loop
INSERT INTO TABLE4 (TYPEID,MANUFACTURERID,LOCATIONID) VALUES TYPEID,MANUFACTURERID,LOCATIONID
EndLoop
EndLoop
EndLoop
我确信我已经看过这件事(在我认为的程序中),但是我找不到这个例子,任何帮助都会非常感激。
答案 0 :(得分:2)
您只需要cross join
:
insert into table4 (TYPEID,MANUFACTURERID,LOCATIONID)
select
t1.TYPEID,
t2.MANUFACTURERID,
t3.LOCATIONID
from (
SELECT DISTINCT TYPEID FROM TABLE1
) t1 cross join (
SELECT DISTINCT MANUFACTURERID FROM TABLE2
) t2 cross join (
SELECT DISTINCT LOCATIONID FROM TABLE3
) t3;
或
insert into table4 (TYPEID,MANUFACTURERID,LOCATIONID)
select distinct
t1.TYPEID,
t2.MANUFACTURERID,
t3.LOCATIONID
from table1 t1
cross join table2 t2
cross join table3 t3
答案 1 :(得分:0)
不需要循环:
INSERT INTO TABLE4 (TYPEID,MANUFACTURERID,LOCATIONID)
SELECT DISTINCT TABLE1.TYPEID, TABLE2.MANUFACTURERID, TABLE3.LOCATIONID
FROM TABLE1
CROSS JOIN TABLE2
CROSS JOIN TABLE3;