1

谁能告诉我如何做到这一点......


    Table 1                                 Table 2
      Cat_ID   Cat_Name                        Term_ID     Term_Name
       1       ab                                 1986       January 2013
       2       cd                                 1987       February 2013
       3       ef                                 1988       March 2013
       4       gh

我希望输出为:


   Table 3 
    Term_ID     Term_Name          CAT_ID      CAT_Name
    1986       January 2013           1      ab
    1986       January 2013           2      cd
    1986       January 2013           3      ef
    1986       January 2013           4      gh
    1987       February 2013          1      ab
    1987       February 2013          2      cd
    1987       February 2013          3      ef
    1987       February 2013          4      gh
    1988       March 2013             1      ab
    1988       March 2013             2      cd
    1988       March 2013             3      ef
    1988       March 2013             4      gh

我必须把它写成一个 SQL 查询。

4

1 回答 1

5

您可以使用 aCROSS JOIN来获得所需的笛卡尔结果:

select t2.term_id, 
  t2.term_name, 
  t1.cat_id, 
  t1.cat_name
from table1 t1
cross join table2 t2

请参阅SQL Fiddle with Demo。获得结果后,您可以将数据插入table3

insert into table3 (term_id, term_name, cat_id, cat_name)
select t2.term_id, 
  t2.term_name, 
  t1.cat_id, 
  t1.cat_name
from table1 t1
cross join table2 t2
于 2013-06-17T14:59:56.277 回答