MySQL - 如何选择MAX日期的行

时间:2014-08-06 12:54:11

标签: php mysql date max

我的表是:

id_clocking | project_id | date       | comments        | next_steps | dependencies
1           | 39         | 2014-08-01 | new_com         | step1      | dependency1
2           | 39         | 2014-08-10 | NULL            | step2      | NULL
3           | 39         | 2014-08-12 | NULL            | step3      | dependencyx
4           | 39         | 2014-08-05 | commentx        | stepx      | depend
5           | 42         | 2014-07-15 | NULL            | NULL       | dependency_3
6           | 42         | 2014-07-03 | comment_3       | step_c     | dependency_4
7           | 42         | 2014-08-01 | NULL            | step_d     | NULL            

我需要通过选择最新的注释,next_steps,依赖于每个project_id的日期的最大值来从MySQL数据库创建PHP输出

结果将是:

 project_id | comments  | next_steps | dependencies
 ------------ ------------------------------------
    39      | commentx  | step3      | dependencyx
    42      | comment_3 | step_d     | dependency_3

并且应该仅返回具有基于可用的最高日期的值的行(我们在哪里-----没有值,并且不应该考虑行) 你能分享一下你的想法吗?

4 个答案:

答案 0 :(得分:1)

我怀疑这是非常错综复杂的 - 但我似乎有一个高级时刻,所以......

 DROP TABLE IF EXISTS my_table;

 CREATE TABLE my_table
 (id_clocking INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,project_id INT NOT NULL
 ,date        DATE NOT NULL
 ,comments        VARCHAR(20) NULL
 ,next_steps VARCHAR(20) NULL
 ,dependencies VARCHAR(20) NULL
 );

 INSERT INTO my_table VALUES
 (1,39,'2014-08-01','new_com','step1','dependency1'),
 (2,39,'2014-08-10',NULL,'step2',NULL),
 (3,39,'2014-08-12',NULL,'step3','dependencyx'),
 (4,39,'2014-08-05','commentx','stepx','depend'),
 (5,42,'2014-07-15',NULL,NULL,'dependency_3'),
 (6,42,'2014-07-03','comment_3','step_c','dependency_4'),
 (7,42,'2014-08-01',NULL,'step_d',NULL);

 SELECT i.project_id
      , j.comments
      , k.next_steps
      , l.dependencies
   FROM 
      ( SELECT project_id
             , MAX(CASE WHEN type = 'comment' THEN max_date END) comment_date
             , MAX(CASE WHEN type = 'next_steps' THEN max_date END) next_steps_date
             , MAX(CASE WHEN type = 'dependencies' THEN max_date END) dependencies_date
          FROM 
             ( SELECT a.project_id
                    , a.type
                    , MAX(a.date) max_date
                 FROM 
                    ( SELECT project_id
                           , date
                           , 'comment' type
                           , comments FROM my_table 
                       WHERE comments IS NOT NULL
                       UNION
                      SELECT project_id
                           , date
                           , 'next_steps' 
                           , next_steps 
                        FROM my_table 
                       WHERE next_steps IS NOT NULL
                       UNION
                      SELECT project_id
                           , date
                           , 'dependencies' 
                           , dependencies 
                        FROM my_table 
                       WHERE dependencies IS NOT NULL
                    ) a
                GROUP
                   BY project_id
                    , type
             ) m
         GROUP
            BY project_id
      ) i
   JOIN my_table j
     ON j.project_id = i.project_id 
    AND j.date = i.comment_date
   JOIN my_table k
     ON k.project_id = i.project_id 
    AND k.date = i.next_steps_date
   JOIN my_table l
     ON l.project_id = i.project_id 
    AND l.date = i.dependencies_date;

 +------------+-----------+------------+--------------+
 | project_id | comments  | next_steps | dependencies |
 +------------+-----------+------------+--------------+
 |         39 | commentx  | step3      | dependencyx  |
 |         42 | comment_3 | step_d     | dependency_3 |
 +------------+-----------+------------+--------------+

这似乎是一个更简单的解决方案......(也更快:http://sqlfiddle.com/#!2/b9552e/5

SELECT x.project_id
     , c.comments
     , n.next_steps
     , d.dependencies
  FROM 
     ( SELECT project_id
            , MAX(CASE WHEN comments IS NOT NULL THEN date END) comments_date
            , MAX(CASE WHEN next_steps IS NOT NULL THEN date END) next_steps_date
            , MAX(CASE WHEN dependencies IS NOT NULL THEN date END) dependencies_date 
         FROM my_table 
        GROUP 
           BY project_id
     ) x
  JOIN my_table c
    ON c.project_id = x.project_id
   AND c.date = x.comments_date
  JOIN my_table n
    ON n.project_id = x.project_id
   AND n.date = x.next_steps_date
  JOIN my_table d
    ON d.project_id = x.project_id
   AND d.date = x.dependencies_date
 WHERE c.comments IS NOT NULL
   AND n.next_steps IS NOT NULL
   AND d.dependencies IS NOT NULL;

   +------------+-----------+------------+--------------+
   | project_id | comments  | next_steps | dependencies |
   +------------+-----------+------------+--------------+
   |         39 | commentx  | step3      | dependencyx  |
   |         42 | comment_3 | step_d     | dependency_3 |
   +------------+-----------+------------+--------------+

答案 1 :(得分:1)

我理解你的问题,你想要每个project_id

  • 最新的评论项目非空
  • 最新的next_steps项目非空
  • 最新的非空依赖项

这些项目可以来自不同的行。如果是这种情况,您可以使用此查询执行此操作:

SELECT
    project_id,
    (SELECT e2.comments FROM example8 e2 WHERE e2.date = (
        SELECT MAX(date) FROM example8 e1 
        WHERE e1.comments IS NOT NULL and e2.project_id = e1.project_id
        GROUP BY e1.project_id )
     AND e.project_id = e2.project_id) as comments,
    (SELECT e2.next_steps FROM example8 e2 WHERE e2.date = (
        SELECT MAX(date) FROM example8 e1 
        WHERE e1.next_steps IS NOT NULL and e2.project_id = e1.project_id
        GROUP BY e1.project_id )
     AND e.project_id = e2.project_id) as next_steps,
    (SELECT e2.dependencies FROM example8 e2 WHERE e2.date = (
        SELECT MAX(date) FROM example8 e1 
        WHERE e1.dependencies IS NOT NULL and e2.project_id = e1.project_id
        GROUP BY e1.project_id )
     AND e.project_id = e2.project_id) as dependencies
FROM     example8 e
GROUP BY e.project_id;

<强> Demo: Solutions of Strawberry and mine

答案 2 :(得分:-1)

要获得结果,请运行此查询:
SELECT * FROM tableName AS a WHERE date = ( SELECT MAX(date) FROM tableName AS b )

答案 3 :(得分:-1)

下面显示的查询应该有效:

Select 
    tableA.project_id, comments, next_steps, dependencies
from
    tableA,
    (select 
        max(date) date, project_id
    from
        tableA
    group by project_id) tableB
WHERE
    tableA.date = tableB.date
        AND tableA.project_id = tableB.project_id;

请根据需要更改列名和表名。