2011-12-30 56 views
0

我有这样的一个表:选择查找,更新的行和列在SQL

+---------+---------+---------------+ 
| 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。

感谢

一个时髦的查询
+0

不太理解你的数据库设置。 Col1/Col2中的数字是多少? – Aufziehvogel 2011-12-30 17:09:09

+0

听起来像你需要'timestamp1'和'timestamp2'。 – Ben 2011-12-30 17:11:11

+0

@Aufziehvogel:他们是从不同来源获得的同一因素的两个参数,我需要最近改变的其中一个参数。 – RYN 2011-12-30 17:43:54

回答

1

类,但这是什么样子:

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) 

它看起来有点漂亮,如果你正好有你的表的唯一标识符。