mysql:如何获取一个特殊列的最后一个值作为子查询的一部分

时间:2016-03-31 15:45:32

标签: mysql

我有下表。

+--------------------+--------+------+------------+----------+---------------------+
| StopAddress        | UserID | Name | StartHours | EndHours | SamplingEnd         |
+--------------------+--------+------+------------+----------+---------------------+
| Legends            |      1 | Lisa | 09:00:00   | 08:00:59 | 2016-03-22 09:00:00 |
| 26 Bluewater Drive |      1 | Lisa | 10:00:00   | 08:00:59 | 2016-03-22 10:00:00 |
| Walker Drive       |      1 | Lisa | 11:00:00   | 08:00:59 | 2016-03-22 11:00:00 |
| Blouberg           |      1 | Lisa | 12:00:00   | 08:00:59 | 2016-03-22 12:00:00 |
| William Moffet     |      1 | Lisa | 13:00:00   | 08:00:59 | 2016-03-22 13:00:00 |
| Paterson           |      1 | Lisa | 14:00:00   | 08:00:59 | 2016-03-22 14:00:00 |
| 70 Kings Street    |      1 | Lisa | 15:00:00   | 08:00:59 | 2016-03-22 15:00:00 |
| Pick n Pay         |      1 | Lisa | 16:00:00   | 08:00:59 | 2016-03-22 16:00:00 |
| Mars               |      1 | Lisa | 17:00:00   | 08:00:59 | 2016-03-22 17:00:00 |
| Club Shakes        |      1 | Lisa | 18:00:00   | 08:00:59 | 2016-03-22 18:00:00 |
| Stones             |      1 | Lisa | 19:00:00   | 08:00:59 | 2016-03-22 19:00:00 |
| Ermelo             |      1 | Lisa | 20:00:00   | 08:00:59 | 2016-03-22 20:00:00 |
| Angelos            |      1 | Lisa | 05:00:00   | 00:20:16 | 2016-03-23 05:00:00 |
| Game               |      1 | Lisa | 05:00:00   | 00:20:16 | 2016-03-23 04:00:00 |
+--------------------+--------+------+------------+----------+---------------------+

当我运行以下查询时,我得到以下结果。 (参见“当前结果”)

QUERY:

SELECT Min(samplingend) AS StartTime, 
       Min(starthours)  AS StartHours, 
       Max(endhours)    AS EndHours, 
       Max(samplingend) AS LastStart, 
       (SELECT stopaddress 
        FROM   table1 
        ORDER  BY samplingend DESC 
        LIMIT  1)       AS StopAddress 
FROM   table1 
GROUP  BY userid, 
          name, 
          Date(samplingend) 
ORDER  BY Date(samplingend), 
          userid; 

当前结果:

 +---------------------+------------+----------+---------------------+-------------+
 | StartTime           | StartHours | EndHours | LastStart           | StopAddress |
 +---------------------+------------+----------+---------------------+-------------+
 | 2016-03-22 09:00:00 | 09:00:00   | 08:00:59 | 2016-03-22 20:00:00 | Angelos     |
 | 2016-03-23 04:00:00 | 05:00:00   | 00:20:16 | 2016-03-23 05:00:00 | Angelos     |
 +---------------------+------------+----------+---------------------+-------------+

期望的结果:

运行查询时,它必须提供每天分组的StopAddress WHERE StopAddress = Max(SamplingEnd)。

例如在第22个StopAddress应该=“Ermelo”和23日= Angelos在下面的例子中。是否可以通过仅在LINE6中更改代码而不使用表别名来实现此结果。例如,不使用table1 T1或T2?

第6行:(通过SamplingEnd DESC限制1从table1顺序中选择StopAddress)AS StopAddress)

结果示例:(查看StopAddress列)

 +---------------------+------------+----------+---------------------+-------------+
 | StartTime           | StartHours | EndHours | LastStart           | StopAddress |
 +---------------------+------------+----------+---------------------+-------------+
 | 2016-03-22 09:00:00 | 09:00:00   | 08:00:59 | 2016-03-22 20:00:00 | Ermelo     |
 | 2016-03-23 04:00:00 | 05:00:00   | 00:20:16 | 2016-03-23 05:00:00 | Angelos     |
 +---------------------+------------+----------+---------------------+-------------+

感谢您的时间!!

2 个答案:

答案 0 :(得分:0)

您需要包含表别名才能使用外部值

SELECT Min(samplingend) AS StartTime, 
       Min(starthours)  AS StartHours, 
       Max(endhours)    AS EndHours, 
       Max(samplingend) AS LastStart, 
       (SELECT T2.stopaddress 
        FROM   table1  T2
        WHERE  Date(T2.samplingend) = Date(T1.samplingend) -- ADD THIS WHERE
        ORDER  BY T2.samplingend DESC 
        LIMIT  1)       AS StopAddress 
FROM   table1  T1
GROUP  BY userid, 
          name, 
          Date(samplingend) 
ORDER  BY Date(samplingend), 
          userid; 

答案 1 :(得分:0)

试试这个:

SELECT StartTime, StartHours, EndHours, LastStart,
      (SELECT StopAddress 
       FROM table1 AS t2 
       WHERE t1.UserID = t2.UserID AND 
             t1.Name = t2.Name AND 
             t2.SamplingEnd = t1.LastStart
       LIMIT 1) AS StopAddress     
FROM (
  SELECT UserID, Name,
         MIN(SamplingEnd) AS StartTime,
         MIN(StartHours) AS StartHours,
         MAX(EndHours) AS EndHours,
         MAX(SamplingEnd) AS LastStart   
  FROM table1 
  GROUP BY UserID, Name, DATE(SamplingEnd)) AS t1

我们的想法是将您的查询包装在子查询中,并在外部查询中使用相关子查询使用LastStart的值来获取StopAddress

Demo here