0

I want to insert values into a Table_C by cross joining two tables Table_A and Table_B.

Table_A contains two attributes ID and item.
Table_B contains two attributes ID and color.
Table_C contains four attributes ID,item,color,quantity.

All IDs have AUTO INCREMENT.

suppose each item can have all color and I need to create a relation about it. How should I write a query for this? How could I reference a relation cross joining item and color. What my solution is create an intermediate third table joining these two tables and then use that table to insert values into Table_C. But I am pretty sure that there is a better optimized solution for this.

Thanks in advance.

4

2 回答 2

2

不需要临时表......你可以这样做:

insert into ...
select ... from ...

编写您需要“填充”您提到的临时表的查询,并将行直接插入您的最终表中。

于 2013-06-04T09:33:14.117 回答
1

这是对我有用的查询。

INSERT INTO Table_C (SELECT null, Table_A.item, Table_B.color, null FROM 
Table_A CROSS JOIN Table_B);
于 2013-06-04T10:38:47.907 回答