2017-03-08 51 views
1
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= '[email protected]' 
group by h.Name 

我试图做此发言的基础上的内部最新的时间戳返回唯一项(名)参加与UNIQUE_ID另一个表的唯一条目, 但它是不用最大(Stamp)返回每个条目的最新时间戳,有没有人有任何想法,我可能会在这里出错? 我一直在尝试这一段时间。SQL内加入基于最新时间戳

回答

1

可能是你需要选择其中的元组匹配结果FO您选择

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= '[email protected]' 
      group by h.Name) 
+0

嗨scaisEdge,首先关闭谢谢大家的响应速度快,我刚试过,但它unfortuneately它在MySQL没有工作呢 – kelevra88

+0

我可以通过使用ORDER BY Stamp DESC LIMIT 1来得到这个返回一个表中的最新条目;但我还需要包含其他表连接。出于某种原因,max(Stamp)不会以同样的方式为我工作。 – kelevra88

+0

您对我的回答有错误吗? – scaisEdge