Zero Zero - 5 months ago 6
SQL Question

Combined two table with different column in mysql server

I am trying to combine two tables with different column names but I can’t get the right result:

Table 1:

date userID
2015-09-08 23:31:04 3
2015-09-08 23:31:05 3


Table 2:

leave userID
2015-09-08 23:31:10 3


Expected Output:

userID leave date
3 2015-09-08 23:31:10
3 2015-09-08 23:31:04
3 2015-09-08 23:31:05


I used this following query below but the output is not what I need.

SELECT UserID FROM table1
UNION ALL
SELECT UserID FROM table2


My question is it possible to combine the 2 tables with the output I want? If it's possible could you help me how to do it? Thanks

Answer

Try this:

  SELECT UserID, null, `date`
  FROM table1 

  UNION ALL 

  SELECT UserID, `leave`, null
  FROM table2
Comments