我可以从每列获取一些数据,然后使用mysql查询将其存储在一个结果列中吗?
Table
Truck Number, Depart time, Arrive time
0001,'2016-10-05 07:15:00','2016-10-05 10:10:00'
0002,'2016-10-05 08:15:00','2016-10-05 09:30:00'
0003,'2016-10-04 19:15:00','2016-10-05 08:45:00'
我只想要从2016-10-05 08:00:00离开或到达的卡车到'2016-10-05 08:50:00'
我希望输出为
Truck, Time
0002 '2016-10-05 08:15:00'
0003 '2016-10-05 08:45:00'
如果可能的话,列也是时候来的。
答案 0 :(得分:0)
UNION会为你工作吗?,就像这样:
select truck, departtime as time , 'Depart' as direction
where departtime between '2016-10-05 08:00:00' and '2016-10-05 08:50:00'
UNION
select truck, arrivetime as time , 'Arrive' as direction
where arrivetime between '2016-10-05 08:00:00' and '2016-10-05 08:50:00'
ORDER BY TRUCK, TIME
答案 1 :(得分:0)
SELECT T.TRUCKNUMBER,
CASE WHEN DEPART BETWEEN '2016-10-05 08:00:00' AND '2016-10-05 08:50:00' THEN DEPART
ELSE ARRIVE
END AS TIME ,
CASE WHEN DEPART BETWEEN '2016-10-05 08:00:00' AND '2016-10-05 08:50:00' THEN 'DEPART'
ELSE 'ARRIVE'
END AS SourceColumn
FROM T WHERE DEPART BETWEEN '2016-10-05 08:00:00' AND '2016-10-05 08:50:00' OR
ARRIVE BETWEEN '2016-10-05 08:00:00' AND '2016-10-05 08:50:00'
结果
+-------------+---------------------+--------------+
| TRUCKNUMBER | TIME | SourceColumn |
+-------------+---------------------+--------------+
| 2 | 2016-10-05 08:15:00 | DEPART |
| 3 | 2016-10-05 08:45:00 | ARRIVE |
+-------------+---------------------+--------------+