怎样用SELECT查询离2003-10-1日最近的数据?谢谢!

jjaacc 2003-09-14 03:36:32
如题.
...全文
44 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
playyuer 2003-09-14
  • 打赏
  • 举报
回复
select *
from T
where abs(datediff(millisecond,时间字段,'2003-10-1'))
= (select min(abs(datediff(millisecond,时间字段,'2003-10-1'))) from T )
playyuer 2003-09-14
  • 打赏
  • 举报
回复
select *
from T
where abs(datediff(day,时间字段,'2003-10-1'))
= (select min(abs(datediff(day,时间字段,'2003-10-1'))) from T )
playyuer 2003-09-14
  • 打赏
  • 举报
回复
select *
from T
where datediff(day,时间字段,'2003-10-1')
= (select min(datediff(day,时间字段,'2003-10-1')) from T )
zarge 2003-09-14
  • 打赏
  • 举报
回复
-- 按秒
SELECT top 1 * FROM 表 ORDER BY ABS(DATEDIFF(second, '2003-10-01', 时间字段))
-- 按分
SELECT top 1 * FROM 表 ORDER BY ABS(DATEDIFF(minute, '2003-10-01', 时间字段))
-- 按时
SELECT top 1 * FROM 表 ORDER BY ABS(DATEDIFF(hour, '2003-10-01', 时间字段))
-- 按日
SELECT top 1 * FROM 表 ORDER BY ABS(DATEDIFF(day, '2003-10-01', 时间字段))
clxxj 2003-09-14
  • 打赏
  • 举报
回复
select * from 表 where 日期字段 in (select Max(日期字段) from 表 where 日期字段<'2003-10-1')

select tb1.*,tb2.* from table1 tb1 inner join table2 tb2 on tb1.id = tb2.id
where tb1.[日期字段] in (select Max(日期字段) from table1 where 日期字段<'2003-10-1')
aierong 2003-09-14
  • 打赏
  • 举报
回复


declare @n table (i smalldatetime)
insert into @n values('20020101')
insert into @n values('20030101')
insert into @n values('20040101')
insert into @n values('20030901')

select * from @n
where i=(select top 1 * from @n where datediff(day,i,'20031001')>=0 order by i desc)
小苞梁 2003-09-14
  • 打赏
  • 举报
回复
Select *
from table
where table.日期字段=
(Select Max(table.日期字段) from table where table.日期字段<'2003-10-1')
w_rose 2003-09-14
  • 打赏
  • 举报
回复
select top 1 * from 表 order by abs(时间字段-cast('20031001' as datetime))


10月1日我的一个女朋友过生日!
benxie 2003-09-14
  • 打赏
  • 举报
回复
up
tqqonline 2003-09-14
  • 打赏
  • 举报
回复
select *
from table1 a,table2 b
where a.id=b.id and a.日期字段=(Select Max(日期字段) from table1 where 日期字段<'2003-10-1')
jjaacc 2003-09-14
  • 打赏
  • 举报
回复
我是想把两张表中的内容关联想来,但时间字段在一张表中,如果我写上两张表就会提示说"聚合不应出现在WHERE子句中,除非该聚合位于HAVING子句或选择列表所包含的子查询中,并且要对其进行聚合的列是外部引用"
怎么办?
sdhdy 2003-09-14
  • 打赏
  • 举报
回复
select * from 表 where 日期字段 in (select Max(日期字段) from 表 where 日期字段<'2003-10-1')
tqqonline 2003-09-14
  • 打赏
  • 举报
回复
Select * from yourtable
where 日期字段=(Select Max(日期字段) from yourtable where 日期字段<'2003-10-1')
97866 2003-09-14
  • 打赏
  • 举报
回复
Select * from 表 where 日期字段=(Select Max(日期字段) where 日期字段<'2003-10-1')
yujohny 2003-09-14
  • 打赏
  • 举报
回复
select top 1 * from 表 where 时间字段<'2003-10-1' order by 时间字段 desc

34,575

社区成员

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

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