6

我有两张桌子

表一是时间表

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

4 回答 4

10
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 ║
╚═══════════╩═════════╩═════════╝
于 2013-04-20T06:18:07.547 回答
0

您可以使用 :-

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

union all

select 'actual' as Caption,edition,time from actualtime
)x 
于 2013-04-20T06:38:17.483 回答
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

也许这会帮助你

于 2013-04-20T06:15:52.897 回答
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 |

看到这个 SQLFiddle

于 2013-04-20T06:17:52.667 回答