表register为用户注册信息表,里面有个datetime类型的字段TIME用于记录注册时间,现在想查询最早和最晚注册的用户信息。我写的sql语句:select * from register where TIME IN (select max(TIME),min(TIME) from register)
3 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
不明白你的需求,如果是取两个时间点之间的所有数据:
select * from register where TIME Between (select max(TIME) from register) and (select min(TIME) from register)
吃鸡游戏
TA贡献1829条经验 获得超7个赞
这种情况,最好是UNION ALL:
SELECT * FROM [register] WHERE [time] = (select max([TIME]) from [register])
UNION ALL
SELECT * FROM [register] WHERE [time] = (select min([TIME]) from [register])
还要注意给time字段建个索引。
慕妹3242003
TA贡献1824条经验 获得超6个赞
SQL语句要讲求效率。UNION的方式效率更高
SELECT * FROM [register] WHERE [time] = (select max([TIME]) from [register])
UNION
SELECT * FROM [register] WHERE [time] = (select min([TIME]) from [register])
- 3 回答
- 0 关注
- 189 浏览
添加回答
举报
0/150
提交
取消