Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two MYSQL table with same column Name

Tags:

mysql

I have two table

table one is scheduletime

id    |   edition    | time   | 
1     |       1      | 9:23am |
2     |       2      | 10:23am|

table two is actualtime

id    | edition  | time    |
1     |    1     | 10:23am |
2     |    2     | 11:23am |

I want to result as

Caption    | Edition    | Time    |
Scheduleed |    1       |  9:23am |
actual     |    1       |  10:23am |
Scheduleed |    2       |  10:23am |
actual     |    2       |  11:23am |

How can do this in MySQL ?

like image 927
noushad Avatar asked Apr 20 '13 06:04

noushad


People also ask

How to concatenate two values from the same column in MySQL?

For this, you can use group_concat () with aggregate function. Let us first create a table − Here is the query to concatenate 2 values from the same column with different conditions −

How to link or join two tables in MySQL?

You can easily link or “join” two or tables in MySQL with the JOIN clause that combines the rows from several tables based on a column that is common to all the tables.

Can you join the same table twice in SQL?

Self joins with hierarchical data and multiple relationships between two tables are just two of the situations for which you need to join the same table twice. There are others; generally, they involve adding one or more columns to a result set from the same table in the same column.

How do you join two tables in a tableau?

There are four easy ways to join two or more tables: Inner Join. Left Join. Right Join. Union. Cross Join or Cartesian Product. Self Join. FULL OUTER JOIN.


1 Answers

SELECT  Caption, Edition, Time
FROM
        (
            SELECT  'Scheduled' Caption, Edition, time
            FROM    scheduleTime
            UNION   ALL
            SELECT  'Actual' Caption, Edition, time
            FROM    scheduleTime
        ) subquery
ORDER   BY Edition, FIELD(Caption, 'Scheduled', 'Actual')
  • SQLFiddle Demo
  • SQLFiddle Demo (without using FIELD(), just plain ORDER BY...DESC)

OUTPUT

╔═══════════╦═════════╦═════════╗
║  CAPTION  ║ EDITION ║  TIME   ║
╠═══════════╬═════════╬═════════╣
║ Scheduled ║       1 ║ 9:23am  ║
║ Actual    ║       1 ║ 9:23am  ║
║ Scheduled ║       2 ║ 10:23am ║
║ Actual    ║       2 ║ 10:23am ║
╚═══════════╩═════════╩═════════╝
like image 176
John Woo Avatar answered Oct 07 '22 16:10

John Woo