求一查询语句

stevenhzj 2008-07-26 11:13:25
create table temp_1
(
id int identity(1,1) not null,
name
telno varchar(20) not null,

begin_time datetime null,
end_time datetime null,

)

insert into temp_1(name,telno,begin_time,end_time)
select 'admin','8246','2008-06-13 00:55:53.000','2008-06-13 23:55:53.000'
union
select 'admin2','8246','2008-06-14 00:55:53.000','2008-06-14 23:55:53.000'
union
select 'admin3','8246','2008-06-15 00:55:53.000','2008-06-15 23:55:53.000'

create table temp_2
(
id int identity(1,1) not null,
telno varchar(20),
u_time datetime null,
)

insert into temp_2(telno,u_time)
select '8246','2008-06-13 12:55:53.000'
union
select '8246','2008-06-13 14:55:53.000'
union
select '8246','2008-06-14 12:55:53.000'
union
select '8246','2008-06-15 12:55:53.000'


现在想得到这样的结果:

admin 8246 2008-06-13 12:55:53.000
admin 8246 2008-06-13 14:55:53.000
admin2 8246 2008-06-14 12:55:53.000
admin3 8246 2008-06-15 12:55:53.000
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mustudent 2008-07-26
  • 打赏
  • 举报
回复
selct a.name,a.telno,b.u_time from temp_1 a inner join temp_2 b on a.telno=b.telno where a.begin_time<=b.u_time and a.end_time>=b.u_time
--try
mustudent 2008-07-26
  • 打赏
  • 举报
回复
selct a.name,a.telno,b.u_time from temp_1 a inner join temp_2 b on a.telno=b.telno
stevenhzj 2008-07-26
  • 打赏
  • 举报
回复
楼上的兄弟没看清要求,

现在想得到这样的结果:

admin 8246 2008-06-13 12:55:53.000
admin 8246 2008-06-13 14:55:53.000
admin2 8246 2008-06-14 12:55:53.000
admin3 8246 2008-06-15 12:55:53.000

这里的时间是 temp_2 的时间,也就是就 temp_2 要根据 temp_1 中的时间段,来判断
结果中的 name 是哪个。
tianhuo_soft 2008-07-26
  • 打赏
  • 举报
回复
selct a.name,a.u_timetelno,b.u_time from temp_1 a left join temp_2 b on a.telno=b.telno

qqhmitzk 2008-07-26
  • 打赏
  • 举报
回复

create table temp_1
([name] varchar(20) not null,
telno varchar(20) not null,

begin_time datetime null,
end_time datetime null,

)

insert into temp_1([name],telno,begin_time,end_time)
select 'admin','8246','2008-06-13 00:55:53.000','2008-06-13 23:55:53.000'
union
select 'admin2','8246','2008-06-14 00:55:53.000','2008-06-14 23:55:53.000'
union
select 'admin3','8246','2008-06-15 00:55:53.000','2008-06-15 23:55:53.000'

create table temp_2
(
id int identity(1,1) not null,
telno varchar(20),
u_time datetime null,
)

insert into temp_2(telno,u_time)
select '8246','2008-06-13 12:55:53.000'
union
select '8246','2008-06-13 14:55:53.000'
union
select '8246','2008-06-14 12:55:53.000'
union
select '8246','2008-06-15 12:55:53.000'
select a.name,a.telno,b.u_time from temp_1 a
inner join temp_2 b on a.telno=b.telno where b.u_time between a.begin_time and a.end_time
/*
name telno u_time
-------------------- -------------------- ------------------------------------------------------
admin 8246 2008-06-13 12:55:53.000
admin 8246 2008-06-13 14:55:53.000
admin2 8246 2008-06-14 12:55:53.000
admin3 8246 2008-06-15 12:55:53.000

(所影响的行数为 4 行)
*/

34,590

社区成员

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

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