我有两张桌子:
things
:+------+----------------+------------+ | id | current_status | created_at | +------+----------------+------------+ | 7770 | active | 2016-08-09 | +------+----------------+------------+
thing_status_tract
:+----------+-------------+-----------+---------------------+ | thing_id | status_from | status_to | changed_at | +----------+-------------+-----------+---------------------+ | 7770 | incomplete | inactive | 2016-08-09 16:26:22 | | 7770 | inactive | active | 2016-08-10 12:31:04 | +----------+-------------+-----------+---------------------+
我需要以下形式的数据。此表具有特定状态及其相应的开始和结束时间戳thing_id
:
+----------+-------------+---------------------+---------------------+
| thing_id | status | status_start_date | status_end_date |
+----------+-------------+---------------------+---------------------+
| 7770 | incomplete | 2016-08-09 00:00:00 | 2016-08-09 16:26:22 |
| 7770 | inactive | 2016-08-09 16:26:22 | 2016-08-10 12:31:04 |
| 7770 | active | 2016-08-10 12:31:04 | now() |
+----------+-------------+---------------------+---------------------+
如何使用 SQL 查询来做到这一点?