Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FULL OUTER JOIN or UNION?

Tags:

sql

db2

confused how to achieve this maybe with FULL OUTER or a UNION. I want to join results in such a way that

Table1                     Table2
---------------           ----------------- 
ID  Name Salary           ID    Fruits
---------------           ----------------- 
1   John   1000           1     Apples 
1   Henry  4000           1     Mangoes 
1   Smith  1000           1     Tomatoes

Result should be

ResultTable
       ------------------------
       ID Name  Salary  Fruits
       -----------------------   
       1  John  1000    Apples
       1  John  1000    Mangoes
       1  John  1000    Tomatoes
       1  Henry 4000    Apples
       1  Henry 4000    Mangoes
       1  Henry 4000    Tomatoes
       1  Smith 1000    Apples
       1  Smith 1000    Mangoes
       1  Smith 1000    Tomatoes 
like image 678
Zo Has Avatar asked Dec 03 '22 11:12

Zo Has


2 Answers

You need a cartesian product join or Cross Join ..

SELECT 
  *
FROM
  table1, table2

or

SELECT
  * 
FROM 
  table1 CROSS JOIN table2

(reference: http://publib.boulder.ibm.com/iseries/v5r2/ic2924/index.htm?info/sqlp/rbafymstcrojo.htm)

like image 101
Gabriele Petrioli Avatar answered Dec 11 '22 17:12

Gabriele Petrioli


use cross join if db2 has it

like image 27
garik Avatar answered Dec 11 '22 15:12

garik