SQL内部联接基于最新时间戳的唯一条目

时间:2017-03-08 19:41:30

标签: mysql sql

SELECT h.Id,h.Name,max(h.Stamp),u.email 
FROM schema.heating h 
inner JOIN  schema.users u ON h.unique_id = u.unique_id 
where u.email= 'example@example.com' 
group by h.Name

我正在尝试使此语句返回基于内部联接的最新时间戳的唯一条目(名称)以及unique_id上​​的另一个表, 但是它没有为每个条目返回最新的时间戳(max)(邮票),是否有人有任何想法,我可能会在这里出错? 我已经尝试了一段时间了。

1 个答案:

答案 0 :(得分:1)

您可能需要选择与您选择的结果匹配的元组

  select * 
  from schema.heating h 
  where (h.Name, h.Stamp) in  ( 
            SELECT h.Name, max(h.Stamp)
            FROM schema.heating h 
            inner JOIN  schema.users u ON h.unique_id = u.unique_id 
            where u.email= 'example@example.com' 
            group by h.Name ) 
相关问题