表register为用户注册信息表,里面有个datetime类型的字段TIME用于记录注册时间,现在想查询最早和最晚注册的用户信息。 我写的sql语句: select * from register where TIME IN (select max(TIME),min(TIME) from register)
慕哥9229398
浏览 190回答 3
3回答
慕神8447489
不明白你的需求,如果是取两个时间点之间的所有数据:select * from register where TIME Between (select max(TIME) from register) and (select min(TIME) from register)
这种情况,最好是UNION ALL:SELECT * FROM [register] WHERE [time] = (select max([TIME]) from [register])UNION ALLSELECT * FROM [register] WHERE [time] = (select min([TIME]) from [register])还要注意给time字段建个索引。
SQL语句要讲求效率。UNION的方式效率更高SELECT * FROM [register] WHERE [time] = (select max([TIME]) from [register])UNIONSELECT * FROM [register] WHERE [time] = (select min([TIME]) from [register])