连续获取mysql中的最后一个非空单元格

时间:2017-07-20 07:43:08

标签: mysql

我有一个以下结构的表格:

id       performance_id     employee_comments   manager_comments
1        23                 NULL                Well done.
2        46                 NULL                Improve Speed
3        46                 Trying to improve   NULL
4        46                 NULL                Put more effort

在上述结构中,一次只有一个注释,employee_comments或manager_comments,而不是同时存在。

我想在一行中获取最后一个非空员工评论和manager_comments。

我需要mysql查询才能将输出显示为:

performance_id     employee_comments   manager_comments
23                 NULL                Well done.
46                 Trying to improve   Put more effort            

3 个答案:

答案 0 :(得分:1)

根据performance_id,您可以获得每个员工评论和经理评论的最大id

SELECT performance_id, 
  MAX(CASE WHEN employee_comments IS NOT NULL THEN id END) AS emp_id,
  MAX(CASE WHEN manager_comments IS NOT NULL THEN id END) AS mgr_id
FROM MyTable
GROUP BY performance_id

上面只返回每个performance_id一行,有两个id号。这些是"最后"条目,由更大的id号决定。

输出:

+----------------+--------+--------+
| performance_id | emp_id | mgr_id |
+----------------+--------+--------+
|             23 |   NULL |      1 |
|             46 |      3 |      4 |
+----------------+--------+--------+

使用该结果,您可以将其连接回表格以检索其他列:

SELECT t.performance_id, 
    t.employee_comments,
    t.manager_comments
FROM (
    SELECT performance_id, 
      MAX(CASE WHEN employee_comments IS NOT NULL THEN id END) AS emp_id,
      MAX(CASE WHEN manager_comments IS NOT NULL THEN id END) AS mgr_id
    FROM MyTable
    GROUP BY performance_id
) AS x
JOIN MyTable AS t ON t.id IN (emp_id, mgr_id)

输出:

+----------------+-------------------+------------------+
| performance_id | employee_comments | manager_comments |
+----------------+-------------------+------------------+
|             23 | NULL              | Well done.       |
|             46 | Trying to improve | NULL             |
|             46 | NULL              | Put more effort  |
+----------------+-------------------+------------------+

以上每个performance_id最多返回两行。您可以使用另一个GROUP BY将它们强制为一行:

SELECT t.performance_id, 
    MAX(t.employee_comments) AS employee_comments,
    MAX(t.manager_comments) AS manager_comments
FROM (
    SELECT performance_id, 
      MAX(CASE WHEN employee_comments IS NOT NULL THEN id END) AS emp_id,
      MAX(CASE WHEN manager_comments IS NOT NULL THEN id END) AS mgr_id
    FROM MyTable
    GROUP BY performance_id
) AS x
JOIN MyTable AS t ON t.id IN (emp_id, mgr_id)
GROUP BY performance_id

输出:

+----------------+-------------------+------------------+
| performance_id | employee_comments | manager_comments |
+----------------+-------------------+------------------+
|             23 | NULL              | Well done.       |
|             46 | Trying to improve | Put more effort  |
+----------------+-------------------+------------------+

答案 1 :(得分:0)

这可以通过MySQL中的嵌套查询来完成。我们必须通过performance_id

进行工会和小组
 SELECT performance_id ,MAX(employee_comments) 
employee_comments,MAX(manager_comments) manager_comments 
FROM(
SELECT performance_id,employee_comments,manager_comments FROM MyTable 
    WHERE id IN (SELECT * FROM 
            (SELECT id FROM MyTable WHERE manager_comments IS NOT NULL ORDER BY id DESC)AS t)
    UNION
    SELECT performance_id,employee_comments,manager_comments FROM MyTable 
    WHERE id IN (SELECT * FROM 
            (SELECT id FROM MyTable WHERE employee_comments IS NOT NULL ORDER BY id DESC) AS p)) g  GROUP BY performance_id;

上述查询应该用于获取最后一个非空员工评论和manager_comments 我试过并测试了它......

答案 2 :(得分:0)

尝试此查询

SELECT t.id,t.performance_id,t2.employee_comments,t3.manager_comments FROM test1  t join test1 t2 on t2.employee_comments IS NOT NULL and t.manager_comments IS NULL
join test1 t3 on t3.manager_comments IS NOT NULL and t.id < t3.id 
group by t.performance_id