mysql - SQL 内部加入基于最新时间戳的唯一条目

标签 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(Stamp) 的条目返回最新的时间戳,有人知道我在这里可能出错的地方吗? 我已经尝试了一段时间了。

最佳答案

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

  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 ) 

关于mysql - SQL 内部加入基于最新时间戳的唯一条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42680497/

相关文章:

php - 使用 PHP 不工作创建数据库记录

Java:Clob 到 byte[]

Java Derby 数据库删除表中的所有行

sql - postgres 更改表并插入行概率

mysql - Postgres 缓慢且延迟的插入

php - 将两个数组组合成关联数组,问题

mysql - 一般错误 : 1267 Illegal mix of collations

SQL 查询从列中获取空值计数

Mysql只选择父级存在的行

mysql id (auto_increment) 相关查询