Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL union of two tables with different columns

Tags:

sql

postgresql

I want to get one result set of rows back from two tables with different column names, one line per line in each table.

Result should look like this, blanks can be null, team_id in 2nd half comes from coach_id:

-----------------------------------------
player_id | team_id | score | improvement
-----------------------------------------
11          20         5
11          21         4
12          22         2
12          23         2
11          20                   5
11          21                   6
12          21                   5
13          23                   10

Here's the schema:

CREATE TABLE coaches
    (`id` int, `team_id` int)
;

INSERT INTO coaches
    (`id`, `team_id`)
VALUES
    (1, 20),
    (2, 21),
    (3, 22),
    (4, 23)
;

CREATE TABLE players
 (`id` int, `player_id` int);


INSERT INTO players
(`id`, `player_id`)
VALUES
(1,11),
(2,12),
(3,13),
(4,14)
;
CREATE TABLE games
    (`id` int, `player_id` int, `team_id` int, `score` int)
;

INSERT INTO games
    (`id`, `player_id`, `team_id`, `score`)
VALUES
    (1, 11, 20, 5),
    (2, 11, 21, 4),
    (3, 12, 22, 2),
    (4, 12, 23, 2)
;

CREATE TABLE sessions
    (`id` int, `player_id` int, `coach_id` int, `improvement` int)
;

INSERT INTO sessions
      (`id`, `player_id`, `coach_id`, `improvement`)
VALUES
    (1, 11, 1, 5),
    (2, 11, 2, 6),
    (3, 12, 2, 5),
    (4, 13, 4, 10)
;

Tried this, but it wasn't really close:

SELECT tweets.player_id
      ,tweets.team_id
      ,follows.coach_id 
FROM tweets FULL OUTER JOIN follows ON (1 = 0);
like image 439
mtbomb Avatar asked Apr 21 '15 06:04

mtbomb


People also ask

Can you UNION tables with different columns SQL?

The SQL UNION operator SQL has strict rules for appending data: Both tables must have the same number of columns. The columns must have the same data types in the same order as the first table.

Can you UNION all with different columns?

Using UNION on Multiple FieldsWe can apply UNION on multiple columns and can also order the results using the ORDER BY operator in the end.

Can you UNION tables with different column names?

The columns of joining tables may be different in JOIN but in UNION the number of columns and order of columns of all queries must be same.

How can I merge two tables in SQL without same columns?

Using the “FROM Table1, Table2” Syntax One way to join two tables without a common column is to use an obsolete syntax for joining tables. With this syntax, we simply list the tables that we want to join in the FROM clause then use a WHERE clause to add joining conditions if necessary.


2 Answers

nulnullry this

 SELECT player_id
    ,team_id
    ,score
    ,NULL AS improvement
FROM games
UNION All
SELECT sessions.player_id
    ,coaches.team_id
    ,NULL AS score
    ,sessions.improvement
FROM sessions
INNER JOIN coaches ON coaches.id = sessions.coach_id
like image 164
SimarjeetSingh Panghlia Avatar answered Nov 15 '22 08:11

SimarjeetSingh Panghlia


Something like:

select player_id
     , team_id
     , score
     , cast(null as int) as improvement 
from games 
union all 
select s.player_id
     , c.team_id
     , cast(null as int) as score
     , s.improvement 
from sessions as s 
join coaches as c 
    on s.coach_id = c.id 
order by score

should work

like image 33
Lennart Avatar answered Nov 15 '22 07:11

Lennart