找到maxdate并在分组时注意

时间:2012-06-08 10:56:31

标签: php group-by maxdate mindate

数据库表

TYPE       DATE         PLAYER   MINUTE
---------------------------------------
starter    2000-01-01   Tom      
starter    2005-01-01   Tom      
starter    2005-01-01   Ben 
starter    2010-01-01   Tom
subbedin   2012-01-01   Ben      46

查询

$query = mysql_query("SELECT *,
SUM(CASE WHEN type = 'starter' OR type = 'subbedin' THEN 1 ELSE 0 END) AS apps,
SUM(CASE WHEN Tüüp = 'starter' THEN 90 ELSE 0 end + CASE WHEN Tüüp = 'subbedin' THEN 91 - minutes ELSE 0 end) AS minutes
FROM players
GROUP BY player
ORDER BY apps DESC minutes DESC
");

我将玩家的结果与他们的出场次数和分钟数进行分组,但是我无法显示第一次和最后一次出现的日期。

PLAYER   APPS   MINUTES   FIRST        LAST
-------------------------------------------------
Tom      3      270       2000-01-01   2010-01-01
Ben      2      135       2005-01-01   2012-01-01

1 个答案:

答案 0 :(得分:0)

试试这个......

SELECT
    SUM(CASE WHEN type = 'starter' OR type = 'subbedin' THEN 1 ELSE 0 END) AS apps,
    SUM(CASE WHEN type = 'starter' THEN 90 ELSE 0 end + CASE WHEN type = 'subbedin' THEN 91 - minutes ELSE 0 end) AS `minutes`,
    MIN(`DATE`) as `FIRST`,
    MAX(`DATE`) as `LAST`
    FROM players
    GROUP BY player;