常用SQL查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_18607183/article/details/84936024

1、在多条记录中查询最新的一条

怎么才算最新的一条,根据业务,比如ID大小,或者时间大小等条件而定。

2

3

4

5

6

7

8

9

10

11

12

13

14

15

-- 方法1

select a.* 

 from table1 a

 where not exists(select 

                  from table1 b

                  where b.name=a.name and b.gdtime>a.gdtime)

-- 方法2

select a.*

 from table1 a

 inner join

 (select name,

         max(gdtime) 'maxgdtime'

  from table1 

  group by name) b on a.name=b.name and a.gdtime=b.maxgdtime

猜你喜欢

转载自blog.csdn.net/baidu_18607183/article/details/84936024
今日推荐