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

解决方案 »

  1.   

    selct a.name,a.u_timetelno,b.u_time from temp_1 a left join temp_2 b on a.telno=b.telno
      

  2.   

    楼上的兄弟没看清要求, 现在想得到这样的结果: 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 是哪个。
      

  3.   

    selct a.name,a.telno,b.u_time from temp_1 a inner join temp_2 b on a.telno=b.telno 
      

  4.   

    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
      

  5.   


    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 行)
    */