将两个MYSQL表与相同的列名组合在一起

时间:2013-04-20 06:10:23

标签: mysql

我有两张桌子

表一是计划时间

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

表二是实际时间

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

我希望以

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

如何在MySQL中执行此操作?

4 个答案:

答案 0 :(得分:8)

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')

输出

╔═══════════╦═════════╦═════════╗
║  CAPTION  ║ EDITION ║  TIME   ║
╠═══════════╬═════════╬═════════╣
║ Scheduled ║       1 ║ 9:23am  ║
║ Actual    ║       1 ║ 9:23am  ║
║ Scheduled ║       2 ║ 10:23am ║
║ Actual    ║       2 ║ 10:23am ║
╚═══════════╩═════════╩═════════╝

答案 1 :(得分:0)

select * from
(select 'Scheduleed' as Caption, Edition as Edition, Time as Time from scheduletime

union all

select 'actual' as Caption, Edition as Edition, Time as Time from actualtime) as a

order by Edition

也许这会对你有所帮助

答案 2 :(得分:0)

试试这个:

SELECT 
    'Scheduleed' AS Caption, 
    Edition, 
    time 
FROM scheduletime
UNION ALL
SELECT 
    'actual' AS Caption, 
    Edition, 
    time 
FROM actualtime
ORDER BY Edition, Caption DESC

输出:

|    CAPTION | EDITION |    TIME |
----------------------------------
| Scheduleed |       1 |  9:23am |
|     actual |       1 | 10:23am |
| Scheduleed |       2 | 10:23am |
|     actual |       2 | 11:23am |

See this SQLFiddle

答案 3 :(得分:0)

您可以使用: -

select * from(
select 'Scheduleed' as Caption ,edition,time from scheduletime

union all

select 'actual' as Caption,edition,time from actualtime
)x