如何在数据库中查询出最新日期的记录?

cpzhg 2004-09-20 02:44:09
在表table1中有add_date字段,add_date字段中有2004-09-20,2004-09-19,2004-09-18...的数据,并且每一天的日期都对应很多条记录,我想查询最新一天的数据,sql语句怎么写呀,请高手指点!
...全文
727 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpzhg 2004-09-20
  • 打赏
  • 举报
回复
求出來是可以,但要提取与userid相匹配的add_date字段,现在的问题是求的总个add_date的最大日期,请高手指点!!!
postfxj 2004-09-20
  • 打赏
  • 举报
回复
這個問題好像以前也有人提過,你把最大的日期求出來不就可以了嗎。
cpzhg 2004-09-20
  • 打赏
  • 举报
回复
怎么没人回答呀
cpzhg 2004-09-20
  • 打赏
  • 举报
回复
谢谢大家的帮忙
查最后一天的日期是可以,但现在有一个问题就是在table1表中userid字段必须要跟session("username")相同,sql语句如下:
select * from table1 where userid='"&session("username")&"' and add_date=(select max(add_date) from table1)
这样就造成了userid相同而最新日期就不能匹配了,我必须要userid和add_date同时匹配,如何修改呀
99831323 2004-09-20
  • 打赏
  • 举报
回复
如果你的add_date已经格式化到日期(就象你举例的)

select * from table1 a where add_date=(
select max(add_date) from table1)

如果没有格式化到日期:

select * from table1 a where datediff(day,add_date,(
select max(add_date) from table1))=0

ywh25 2004-09-20
  • 打赏
  • 举报
回复
select id,max(add_date) as date from table1
group by id
99831323 2004-09-20
  • 打赏
  • 举报
回复
select * from table1 a where add_date=(
select max(add_date) from table1)

yyhyy23 2004-09-20
  • 打赏
  • 举报
回复
select *,max(add_date) as maxdate from table1
group by 字段名字
lsxaa 2004-09-20
  • 打赏
  • 举报
回复
select *
from table1
where datediff("d",add_date
,isnull((select max(add_date) from table1),getdate()))=0
yyhyy23 2004-09-20
  • 打赏
  • 举报
回复
select * from table where add_date=
select top 1 add_date from table group by add_date order by add_date
bernice99 2004-09-20
  • 打赏
  • 举报
回复
select *,date=max(add_date) from table1
cpzhg 2004-09-20
  • 打赏
  • 举报
回复
楼上不行呀,我查询的不一定是当天的记录呀,比如说20号没有记录,查询出来的应该是19号的记录呀
bernice99 2004-09-20
  • 打赏
  • 举报
回复
select add_date=max(add_date) as date from table1

WangZWang 2004-09-20
  • 打赏
  • 举报
回复
select col1,col2,..,max(add_date) as date from table1
group by col1,col2,..
yyhyy23 2004-09-20
  • 打赏
  • 举报
回复
select * from table1 where datediff("d",add_date,getdate())=0

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧