這應該會給你你要找的。
create table visit (tid varchar(16), pid varchar(16), date datetime);
insert into visit values
(112, 45, '2012-03-12'),
(124, 12, '2013-04-03'),
(112, 32, '2013-03-12'),
(421, 14, '2014-01-31');
create table place (pid varchar(16), start varchar(16), end varchar(16), starttime datetime, endtime datetime);
insert into place values
(45, 'xyz', 'qwe', '2012-03-12 12:00:13', '2012-03-12 14:30:00'),
(12, 'abc', 'yst', '2013-04-03 15:45:00', '2013-04-13 16:00:00'),
(32, 'tyu', 'jkf', '2012-03-12 15:30:00', '2012-03-12 17:30:00'),
(14, 'nyk', 'por', '2014-01-31 08:30:00', '2014-01-31 10:00:50');
select v.tid,
(select start from place where starttime = min(p.starttime)) as start,
(select end from place where endtime = max(p.endtime)) as end
from visit v
join place p using (pid)
group by v.tid;
+------+-------+------+
| tid | start | end |
+------+-------+------+
| 112 | xyz | jkf |
| 124 | abc | yst |
| 421 | nyk | por |
+------+-------+------+
注1:我的攝製使用MySQL,所以隨意評論/投票了另一個答案,如果標準的SQL從我所看到的變化。
注意#2:您應該在place
表中使用datetime(而不僅僅是時間)來允許隔夜飛行。