Mysql Query使用多个连接,分组和sum()获取最新记录

时间:2018-02-15 04:11:15

标签: mysql

SELECT Task_Entry_Icode, Task_Master_Icode, Work_Progress,SUM(A.Logged_Hours) as logged,B.*

FROM task_entry一个INNER JOIN task_master B on A.Task_Master_Icode = B.Task_Icode

WHERE Task_Entry_Icode IN(

SELECT MAX(Task_Entry_Icode)

FROM task_entry

GROUP BY Task_Master_Icode

);

2 个答案:

答案 0 :(得分:1)

SELECT * FROM task_master INNER JOIN task_entry B ON A.Task_Icode = B.Task_Master_Icode 在哪里Created_On IN(     SELECT MAX(Cre​​ated_On)     FROM task_entry     GROUP BY Task_Master_Icode )和A. A.Task_Created_By =' 7';

  

这适合我,但如果我使用sum()它只返回一个记录

答案 1 :(得分:0)

SELECT 
  B.Task_Entry_Icode, 
  B.Task_Master_Icode, 
  (SELECT Work_Progress FROM task_entry WHERE Task_Master_Icode=B.Task_Master_Icode ORDER BY B.Created_On DESC LIMIT 1 OFFSET 0),
  SUM(B.Logged_Hours)
FROM 
  task_master A INNER JOIN task_entry B ON A.Task_Icode=B.Task_Master_Icode
GROUP BY 
  B.task_master_icode;