MySQL SELECT id of row where GREATEST of MAX entries of several columns

£可爱£侵袭症+ 提交于 2019-12-11 06:16:11

问题


I have this table 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       | 
+----+--------------------------------------+

Now I try to have one SELECT with the following result:

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

That means the MAX of each date column and the ID from the row in which the GREATEST of the MAX results is.

The Query, that brought me nearly to the solution looks like this:

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

But the id i get, is not the expected one. How can I find out which column has the maxdate so I can find out the appendant id?

see fiddle.


回答1:


a way to do it would be to store your date in a user-defined-variable. then you can use it to pull out the id for the largest date

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




回答2:


please note that when you use MAX,MIN,etc in query, you are practically loosing connection between that value and others. This solution down there may be a bit different, then what you expected, but it simply finds greatest values of 3 fields and the compares them as rows.

(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



回答3:


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



回答4:


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;



回答5:


If I'm understanding correctly:

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


来源:https://stackoverflow.com/questions/26738338/mysql-select-id-of-row-where-greatest-of-max-entries-of-several-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!