Full SQL 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 the following:

  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 seems the full amount of the tables without any conditions. I just want to get all columns and all data from all tables, as is.
How to do it? UPD: tables are not related.
In the case where the tables are related to each other: I would use the LEFT or RIGHT JOINT when it was known in advance which table is larger. But this is unknown.

+6
source share
3 answers

Create an index using row_number to use for a 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 
+6
source

Try the following:

 with a as (select *, row_number() over (order by id1) an from tableA), b as (select *, row_number() over (order by id2) bn from tableB), c as (select *, row_number() over (order by id3) cn from tableC) select a.id1, a.data1, b.id2, b.data2, c.id3, c.data3 from a full outer join b on a.an = b.bn full outer join c on a.an = c.cn 

SQL Fiddle

+3
source

I find it impossible to join tables without any conditions. Otherwise, you must place the key from table B with one key from table A. It does not matter if you want to place the key of table B (associated with the key of table A). You can still join it using an SQL query.

It will look like this:

 SELECT tableA.*, tableB.* FROMtableA JOIN tableB ON tableA.id1 = tableB.id1 

see, you can select a field based on desire.

0
source

Source: https://habr.com/ru/post/972304/


All Articles