Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL full join without any conditions

I am using MS SQL.
I have the following tables:

table A:
    id1   data1
    8234    ko
    2       po
    333     koo
    40      woo

table B:
    id2     data2
    123     meow
    654     frrr

table C:
    id3     data3
    10          a
    20          b
    30          c
    40          d
    50          e   
    60          f

I want to get this:

    id1     data1       id2     data2       id3     data3
    8234    ko          123      meow        10         a
    2       po          654      frrr        20         b
    333     koo         NULL     NULL        30         c
    40      woo         NULL     NULL        40         d
    NULL    NULL        NULL     NULL        50         e
    NULL    NULL        NULL     NULL        60         f

It's seems like full sum of tables without any conditions. I just want to get all columns and all data from all tables as is.
How can I do this? UPD: tables are not related.
In case when tables are related: I would use LEFT or RIGHT JOIN, when it was known in advance what table is larger than. But it is unknown.

like image 619
Maksim Nesterenko Avatar asked Jul 16 '14 08:07

Maksim Nesterenko


People also ask

How can I join without any condition in SQL?

We can use 'cross join' without on condition. Cross join gives the result in cartesian product form. For instance, if in one table there are 3 records and another table has 2 records, then the first record will match with all the second table records. Then, the same process will be repeated for second record and so on.

Can we use join without on condition?

When using join or inner join , the on condition is optional. This is different from the ANSI standard and different from almost any other database. The effect is a cross join . Similarly, you can use an on clause with cross join , which also differs from standard SQL.

How do I join two tables without conditions?

Omit the ON clause from the JOIN statement You can just simplly JOIN two tables like this: SELECT * FROM table_a JOIN table_b; It will match each row from table_a to every row in table_b . It's similar to run SELECT * FROM multiple tables statement below.

Can we use LEFT join without on condition?

Any JOIN without an ON clause is a CROSS JOIN. The LEFT JOIN is an outer join, which produces a result set with all rows from the table on the "left" (t1); the values for the columns in the other table (t2) depend on whether or not a match was found.


2 Answers

Create an index using row_number to use for your full join

select * from (
    select 
        row_number() over (order by id1 asc) rn,
        id1, data1
    from ta
) t1    
full join (
    select 
        row_number() over (order by id2 asc) rn,
        id2, data2
    from tb
) t2 on t1.rn = t2.rn
full join (
    select 
        row_number() over (order by id3 asc) rn,
        id3, data3
    from tc
) t3 on t1.rn = t3.rn
like image 148
FuzzyTree Avatar answered Sep 28 '22 20:09

FuzzyTree


Try something like this:

SELECT *
FROM A
    FULL OUTER JOIN B ON 1 = 1
    FULL OUTER JOIN C ON 1 = 1
like image 37
Larry A Avatar answered Sep 28 '22 18:09

Larry A