MySQL SELECT的行ID为GREATEST的几列MAX条目

时间:2014-11-04 15:00:35

标签: mysql select max multiple-columns

我有这张表mytable:

+----+--------------------------------------+
| id | date1      | date2      | date3      |
+----+--------------------------------------+
| 1  | 2014-01-08 | NULL       | NULL       | 
| 2  | 2014-05-09 | NULL       | NULL       | 
| 3  | 2014-06-13 | NULL       | NULL       | 
| 4  | NULL       | 2014-03-24 | NULL       | 
| 2  | NULL       | NULL       | 2014-08-15 | 
| 4  | 2014-01-01 | NULL       | NULL       | 
| 1  | 2014-02-15 | NULL       | NULL       | 
| 3  | NULL       | 2014-12-06 | 2014-10-12 | 
| 4  | 2014-08-06 | NULL       | NULL       | 
| 2  | 2014-05-22 | NULL       | NULL       | 
+----+--------------------------------------+

现在我尝试使用以下结果的一个SELECT:

  id   max_date1    max_date2    max_date3
---------------------------------------------
| 3  | 2014-08-06 | 2014-12-06 | 2014-10-12 | 

这意味着每个日期列的MAX和MAX结果的GREATEST行中的ID。

使我接近解决方案的查询看起来像这样:

SELECT
   id, max(date1), max(date2), max(date3), GREATEST(
     IFNULL(max(date1), 0),
     IFNULL(max(date2), 0),
     IFNULL(max(date3), 0)) AS maxdate
FROM table1

但我得到的身份不是预期的。如何找出哪个列具有maxdate,以便找出附件ID?

see fiddle

5 个答案:

答案 0 :(得分:0)

请注意,当您在查询中使用MAX,MIN等时,实际上会丢失该值与其他值之间的连接。这个解决方案可能会有所不同,然后是您所期望的,但它只是找到3个字段的最大值,并将它们作为行进行比较。

(select id,'date1' as col,(date1) as d from table1 order by d desc limit 1)
UNION
(select id,'date2' as col,(date2) as d from table1 order by d desc limit 1) 
UNION
(select id,'date3' as col,(date3) as d from table1 order by d desc limit 1)
ORDER BY d desc

答案 1 :(得分:0)

SELECT 

  (
  CASE
   WHEN max(t1.date1) >= max(t2.date2) && max(t1.date1) >= max(t3.date3) THEN
    t1.id
   WHEN max(t2.date2) >= max(t1.date1) && max(t2.date2) >= max(t3.date3) THEN
    t2.id
   ELSE
    t3.id
  END) AS  highest_data_row_id,
  max(t1.date1),
  max(t2.date2),
  max(t3.date3)

FROM
  table1 t1, table1 t2, table1 t3

答案 2 :(得分:0)

一种方法是将日期存储在用户定义的变量中。然后你可以用它来取出最大日期的id

SET @A := (SELECT GREATEST(
                     IFNULL(max(date1), 0),
                     IFNULL(max(date2), 0),
                     IFNULL(max(date3), 0)
                  ) FROM table1
           );
-- here i JOIN a select that pulls out the correct id
SELECT t.joinid, max(`date1`), max(`date2`), max(`date3`)
FROM table1
JOIN 
(   SELECT id as joinid 
    FROM table1
    WHERE @A IN -- WHERE my MAX date is in
    (
        SELECT date1 -- here the UNION is just putting all of the dates into one column to compare one date with
        UNION ALL SELECT date2
        UNION ALL SELECT date3
    )
) t -- every table must have an alias

FIDDLE DEMO

答案 3 :(得分:0)

SELECT
    id, max(date1), max(date2), max(date3), GREATEST(
     IFNULL(max(date1), 0),
     IFNULL(max(date2), 0),
     IFNULL(max(date3), 0)) AS maxdate,
     ( select id from 
     (
        select id, max(date1) as temp from table1 group by 1
        union
        select id, max(date2) as temp from table1 group by 1
        union
        select id, max(date3) as temp from table1 group by 1
     ) temptable order by temp desc limit 1) as ok_id
FROM table1;

答案 4 :(得分:0)

如果我理解正确:

http://sqlfiddle.com/#!8/e4f3f/2

 SELECT id, max(date1) As max_date1, max(date2) As max_date2, max(date3) As max_date3 FROM table1 GROUP BY id