我有一个这样的表:
+---------+---------+---------------+
| Col1 | Col2 | timestamp |
+---------+---------+---------------+
| 6050000 | 6030000 | 1325241121990 |
+---------+---------+---------------+
| 6050000 | 6040000 | 1325241611269 |
+---------+---------+---------------+
| 6050000 | 6050000 | 1325248254109 |
+---------+---------+---------------+
| 6060000 | 6050000 | 1325248455780 |
+---------+---------+---------------+
| 6060000 | 6050000 | 1325354237099 |
+---------+---------+---------------+
我需要找出每一行中的哪一个 Col1
或 Col2
相对于之前的行最近进行了更新。
例如上表:
- 第 #2、#3 行
Col2
是最新更新 - 第 4 行、第 5 行
Col1
是最新更新。
问题:如何在 SQL 中找到直到下一条记录出现时列的最后更新的行?
我的目标 DBMS 是 Mysql、SQlite 和 MS Access。
谢谢
最佳答案
有点时髦的查询,但这就是它的样子:
select
case when t1.col1 = t2.col1 then 'Col1 is the same' else 'Col1 is updated' end as Col1Status,
case when t1.col2 = t2.col2 then 'Col2 is the same' else 'Col2 is updated' end as Col2Status
from
table t1
inner join table t2 on
t2.timestamp = (select max(timestamp) from table t3 where t1.timestamp > t3.timestamp)
如果你的 table 上碰巧有一个唯一的标识符,它看起来会更漂亮一些。
关于mysql - 选择在 SQL 中查找更新的列和行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8681924/