MySQL Max(isnull(date_field))或max(coalesce(date_field))

时间:2011-07-08 04:05:49

标签: mysql group-by max coalesce

我有一个查询从父/子关系中读取日期和i 需要根据日期的最大值对记录进行分组 表来了。我的问题:

SELECT child.id as id, 
       MAX(pm.start), 
       MAX(membership.start),
       MAX(IFNULL(pm.start,membership.start)) AS start 
FROM   organisation child
LEFT JOIN organisation pmorg ON child.parent_organisation_id = pmorg.id
LEFT JOIN membership ON child.id = membership.organisation_id
LEFT JOIN membership pm ON pmorg.id = pm.organisation_id

WHERE child.id IN (1,3) AND /*just look at example records*/

    coalesce(pm.start,membership.start) IS NOT NULL AND
    ((membership.active = 1 AND membership.amount_paid > 0.00 ) OR
     (pm.active = 1 AND pm.amount_paid > 0.00))

GROUP BY child.id

我的输出很奇怪:

1       NULL                    2011-07-01 00:00:00 323031312d30372d30312030303a30303a3030
3       2011-07-01 00:00:00     NULL                323031312d30372d30312030303a30303a3030

我需要最后一栏是2011-07-01,我需要两行。任何 想法为什么max(coalesce())不起作用?我正在运行MySQL 5.1

1 个答案:

答案 0 :(得分:0)

愚蠢的是,这个查询只在PHPMyAdmin中断,并且在mysql命令行中完全正常。所以根本没问题!