只有第一个开始日期和最后完成日期

时间:2016-06-06 13:27:31

标签: mysql sql database reporting

我在第一个seminarsessions中有两个数据库表,其中有:

     id     | knowntime    |  
    --------+--------------+ 
       1    | 1            |  
       2    | 0            |  
       3    | 1            |  

在第二个表seminarsessions_date中,每个timestamps都有date,此日期有datefromdateuntil

   id | dateid | seminarsessionid | datefrom | dateuntil
    --------+--------------+------+----------+----------
    1 | 312     | 3               |1462863600| 1462867200
    2 | 123     | 3               |1450944000| 1451206800
    3 | 543     | 3               |1464868800| 1464872400

如何使用已知时间显示each仅次于first 日期last dateuntil

1 个答案:

答案 0 :(得分:0)

您可以使用包含表seminarsessions_date中已聚合的值的派生表来执行此操作:

SELECT t1.id, t1.knowntime, t2.datefrom, t2.dateuntil
FROM seminarsessions
LEFT JOIN (
   SELECT seminarsessionid, 
          MIN(datefrom) AS datefrom,
          MAX(dateuntil) AS dateuntil
   FROM seminarsessions_date
   GROUP BY seminarsessionid 
) AS t2 ON t1.id = t2.seminarsessionid

派生表t2包含第一个 datefrom以及每个dateuntil最后 seminarsessionid值。