MySQL最大日期生成错误的输出

时间:2018-08-18 03:57:03

标签: mysql

我有下表。

 SELECT letter.letter_id, MAX(action.action_date), action.status FROM action
    LEFT JOIN action ON action.letter_id=letter.letter_id
    GROUP BY action.letter_id

我使用以下sql查询获取输出

letter_id   action_date status
1   2018-08-15  on-going
2   2018-08-12  on-going

但是查询生成以下输出。

{{1}}

我不明白我要怎么做。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

对于您的查询,您有两个名为action的表并且没有别名,并且您只需要比较letter_id中的JOIN,还需要比较{{1} }

您可以使用下面的sql来完成

action_date

答案 1 :(得分:0)

您可以在相关子查询中使用聚合函数,如下所示:

  select  * from action t1
   where t1.action_date in     
    (          
     select max(action_date) from action t2  
      where t2.letter=t1.letter
       )

http://sqlfiddle.com/#!9/02c4c/5

id  letter  action_date     status
2   2      2018-08-12       on-going
3   1      2018-08-17       finished