简体   繁体   English

SQL内部联接,按时间戳记在两个表中

[英]SQL inner join, order by timestamp in two tables

How do I order inner join output from the timestamp in two different tables? 如何在两个不同的表中排序时间戳的内部联接输出?

| id |      event |                  timestamp |
------------------------------------------------
|  1 |     passed |        2019-06-05 11:55:44 |
|  1 |     failed |        2019-06-09 08:19:35 |


| id |      event |                  timestamp |
------------------------------------------------
|  1 | email_sent |        2019-06-05 11:56:44 |
|  1 | email_sent |        2019-06-09 08:20:35 |

Desired Result: 所需结果:

| id |      event |                  timestamp |
------------------------------------------------
|  1 |     passed |        2019-06-05 11:55:44 |
|  1 | email_sent |        2019-06-05 11:56:44 |
|  1 |     failed |        2019-06-09 08:19:35 |
|  1 | email_sent |        2019-06-09 08:20:35 |

This is what I have so far, however it orders TableA by the timestamp and then TableB by the timestamp, instead of ordering by all timestamps at once: 这是我到目前为止的内容,但是它按时间戳对TableA进行排序,然后按时间戳对TableB进行排序,而不是按所有时间戳一次进行排序:

SELECT TableA.*, TableB.* FROM TableA 
        INNER JOIN TableB ON TableA.id=TableB.id
        WHERE TableA.id='1' ORDER BY TableA.timestamp, TableB.timestamp

You want union all : 您要union all

select id, event, timestamp 
from tablea
union all
select id, event, timestamp 
from tableb
order by timestamp;

You can add where id = 1 to each subquery, if you want to filter the rows. 如果要过滤行,则可以在每个子查询中添加where id = 1的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM