在工作中,我们经常需要检索出最新条数据,能够实现该功能的sql语句很多,下面列举三个进行效率对比 本次实验的数据表中有55万条数据,以myql为例: 方式1: SELECT * FROM t_device_monitor WHERE uploadTime in(select max(uploadTime) from t_device_monitor) 该语句平均耗时2.3秒 方式2: SELECT * FROM t_device_monitor WHERE uploadTime =(select…
要求:SQL语句按ID以最新时间查询最新的一条记录 方法1: select * from (select *, ROW_NUMBER() over(partition by id order by updateDate desc) as num from table) b where num=1 order by id 方法2: select* from table where updateDate in(select max(updateDate) from table group by id…
创建数据库 创建之前判断该数据库是否存在 if exists (select * from sysdatabases where name='databaseName') drop database databaseName go Create DATABASE databasename on primary-- 默认就属于primary文件组,可省略 ( /*--数据文件的具体描述--*/ name=‘databasename_data’,-- 主数据文件的逻辑名称 filename=‘'所存…
一.关联子查询-查日期最新列 前天在工作中遇到一条非常有用的SQL语句,想了好久愣是没搞出来.今天将这个问题模拟出来:先看表 需求是,对于每个人,仅显示时间最新的那一条记录. 答案如下: select * from record as a where not exists (select null from record as b where a.Name = b.Name and a.CreateTime < b.CreateTime) 结果如下: 这个问题的关键难点在于,既要去除重复,又要显…