mysql - 选择在 SQL 中查找更新的列和行

标签 mysql sql sqlite ms-access

我有一个这样的表:

+---------+---------+---------------+
|   Col1  |   Col2  |   timestamp   |
+---------+---------+---------------+
| 6050000 | 6030000 | 1325241121990 |
+---------+---------+---------------+
| 6050000 | 6040000 | 1325241611269 |
+---------+---------+---------------+
| 6050000 | 6050000 | 1325248254109 |
+---------+---------+---------------+
| 6060000 | 6050000 | 1325248455780 |
+---------+---------+---------------+
| 6060000 | 6050000 | 1325354237099 |
+---------+---------+---------------+

我需要找出每一行中的哪一个 Col1Col2 相对于之前的行最近进行了更新。

例如上表:

  • 第 #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/

相关文章:

php - 如何修改mysql用户密码?

mysql - 根据parrentid从同一张表中获取值

database - 我如何在 sqlite 数据库中使用 Unicode 字符,例如日耳曼元音变音符?

database - 检查数据库是否在 Lua 中打开

date - sql日期排序问题

php - 使用 Kohana Framework 从 2 个数据库中的表中进行选择

mysql - 将新数据库导入旧数据库,如何保留旧数据库的值[PHPMyAdmin]

PHP+MySQL 将 MySQL 数组打印到表中

mysql - SQL语句忽略where参数

swift - UNIQUE 约束失败 : ZTEMPORADA. Z_PK