选择最高和最低日期

时间:2014-04-09 13:02:31

标签: php mysql sql date

我现在有这样的查询:

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`, tag AS `TAG`, value AS `VALUE`, addedTs as `addedTs`
FROM dailyMessage
INNER JOIN `psttodo-uit`
ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
WHERE Code = "010"

我知道得到这样的结果:

DATE        TAG             VALUE                   addedTs 
4 April     calls           3                       2014-04-04 20:32:39
4 April     endTS           2014-04-04 22:40:55     2014-04-04 22:40:55
4 April     kmstart         32                      2014-04-04 18:34:37
4 April     kmstop          34434334                2014-04-04 18:35:30
4 April     startTS         2014-04-04 18:35:00     2014-04-04 18:35:00
4 April     visits          6                       2014-04-04 22:40:53
5 April     endTS           2014-04-05 01:37:41     2014-04-05 01:37:41
5 April     kmstart         30                      2014-04-05 01:37:41
5 April     startTS         2014-04-05 01:13:25     2014-04-05 01:13:25
5 April     visits          1                       2014-04-05 01:13:23
7 April     authorizations  4                       2014-04-07 22:34:37
7 April     calls           9                       2014-04-07 23:18:28
7 April     endTS           2014-04-07 23:35:32     2014-04-07 23:35:32
7 April     kmstart         42                      2014-04-07 18:10:43
7 April     kmstop          55                      2014-04-07 19:37:42
7 April     startTS         2014-04-07 10:45:18     2014-04-07 10:45:18
7 April     visits          6                       2014-04-07 23:35:31
8 April     endTS           2014-04-08 11:24:28     2014-04-08 11:24:28
8 April     startTS         2014-04-08 11:24:28     2014-04-08 11:24:28

现在我还要从psttodo-uit表中选择最高日期。这就是我想要做的事情:

psttodo-uit表格中有一个Hostess Code== Code in my WHERE clausule)。 datetype afgewerkt tablet datum表中还有一个datetime字段。

日期变化如下:

2014-04-04 11:41:22
2014-04-03 17:20:01
2014-04-01 15:12:08
2014-04-07 22:20:47
2014-04-08 11:21:18

现在,当我进行查询时,我想选择特定日期的最长时间。我试过这样的话:

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`, tag AS `TAG`, value AS `VALUE`, addedTs as `addedTs`, MAX(`afgewerkt tablet datum`) AS `TimeSorE`
FROM dailyMessage
INNER JOIN `psttodo-uit`
ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
WHERE Code = "010"

但后来我明白了:

4 April calls   3   2014-04-04 20:32:39 2014-04-09 11:49:42

两个问题:

  • 我只收到一个结果
  • 日期不是4月4日

有人可以帮我解决这个问题吗?

更新

我尝试了什么:

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`, tag AS `TAG`, value AS `VALUE`, addedTs as `addedTs`, MAX(`afgewerkt tablet datum`) AS `TimeSorE`
FROM dailyMessage
INNER JOIN `psttodo-uit`
ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
WHERE Code = "010"
GROUP BY DATE_FORMAT(`Datum`, '%e %M')

给我这个:

4 April calls   3   2014-04-04 20:32:39 2014-04-09 15:46:04
5 April endTS   2014-04-05 01:37:41 2014-04-05 01:37:41 2014-04-09 15:46:04
7 April authorizations  4   2014-04-07 22:34:37 2014-04-09 15:46:04
8 April endTS   2014-04-08 11:24:28 2014-04-08 11:24:28 2014-04-09 15:46:04

但是我需要它们(包括所有标签)。

3 个答案:

答案 0 :(得分:1)

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`, GROUP_CONCAT(tag) AS `TAG`, value AS `VALUE`, addedTs as `addedTs`, MAX(`afgewerkt tablet datum`) AS `TimeSorE`
FROM dailyMessage
INNER JOIN `psttodo-uit`
ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
WHERE Code = "010"
GROUP BY DATE_FORMAT(`Datum`, '%e %M')

如果您需要将结果集作为单独的行,请尝试使用

SELECT A.DATE ,A.CODE,TAG,addedTs,TimeSorE
(
SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`,dailyMessage.Code AS CODE
  MAX(`afgewerkt tablet datum`) AS `TimeSorE`
    FROM dailyMessage
    INNER JOIN `psttodo-uit`
    ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
    WHERE Code = "010"
    GROUP BY DATE_FORMAT(`Datum`, '%e %M')
) A , dailyMessage B

WHERE
A.CODE = B.CODE AND
A.DATE = B. DATE_FORMAT(`Datum`, '%e %M')

答案 1 :(得分:0)

这对我来说绝对有用:

SELECT 
  DATE_FORMAT(`Date`, '%e %M') AS `DATE`, 
  tag AS `TAG`, 
  value AS `VALUE`, 
  addedTs as `addedTs`, 
  MAX(`afgewerkttabletdatum`) AS `TimeSorE`
FROM Test
GROUP BY DATE_FORMAT(`Date`, '%e %M')

以下是一个例子:

SQLFiddle Demo

答案 2 :(得分:0)

我认为这会奏效:

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`,
       tag AS `TAG`,
       value AS `VALUE`,
       addedTs AS `addedTs`,
       t2.`TimeSorE`
FROM dailyMessage
INNER JOIN (SELECT `Hostess Code`,
                   MAX(`afgewerkt tablet datum`) AS `TimeSorE`
            FROM `psttodo-uit`
            GROUP BY `Hostess Code`) t2 ON dailyMessage.Code = t2.`Hostess Code`
WHERE Code = "010"