sql如何根据时间戳取出最新的数据记录

wylc123 1年前 ⋅ 2145 阅读

1-如何根据时间取出最新的数据记录

例子:table1 :
userCode   name            datetime
107        tom            2017/6/21 22:34
107        tom            2017/6/24 10:21
107   tom            2017/12/7 10:45
107   tom            2017/1/15 14:01
107   tom           2017/12/26 14:11
208   jack            2017/6/21 22:36
208   jack          2017/11/15 10:46
208   jack            2017/1/19 9:12
208   jack            2017/1/10 13:57
208   jack           2017/1/22 10:08
309   ben           2017/6/22 12:54
309   ben           2017/3/11 9:16
309   ben           2017/1/10 11:18
309   ben           2017/12/20 15:09

select a.*
from table1 a
where not exists(select 1
from table1 b
where b.name=a.name and b.datetime>a.datetime);


select 1 from table ---查询符合条件的记录的行数

 

或者:

SELECT
a.*
FROM
table1 a
WHERE
( SELECT count(id) FROM table1 b WHERE b.NAME = a.NAME AND b.datetime > a.datetime )=0;

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: