请教一SQL跨表查询

wzcaicai1 2008-10-27 12:11:10
查询 AAA数据库中表1 表2
userid name add code
enddt 日期为2008-11-01至2009-11-01

表1 user
userid enddt
66 2008-08-04
22 2008-11-01
11 2010-11-01

表2 info
userid name add code

44 a1 1 2
22 b4 2 5
11 c3 6 4

--------------------
结果为
userid name add code
22 b4 2 5
...全文
244 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
百年树人 2008-10-27
  • 打赏
  • 举报
回复
select a.* 
from info a, user b
where a.userid = b.userid and convert(varchar(10),b.enddt,120) between '2008-11-01' and '2009-11-01'
samson_www 2008-10-27
  • 打赏
  • 举报
回复
select a.* from inf0 a where exists(select 1 from [user] b where (enddt between '2008-11-01' and '2009-11-01') and a.userid=b.userid)
lucky 2008-10-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 guangshi007 的回复:]
select info.* from info ,user
where enddt between (2008-11-01) and (2008-11-01)and info.userid =user.userid[/Quote]
忘记连接条件了,汗。。。。。。
lucky 2008-10-27
  • 打赏
  • 举报
回复
select info.* from info ,user
where enddt between (2008-11-01) to (2008-11-01)
pt1314917 2008-10-27
  • 打赏
  • 举报
回复
--> 测试数据: @user
declare @user table (userid int,enddt datetime)
insert into @user
select 66,'2008-08-04' union all
select 22,'2008-11-01' union all
select 11,'2010-11-01'
--> 测试数据: @info
declare @info table (userid int,name varchar(2),[add ]int,code int)
insert into @info
select 44,'a1',1,2 union all
select 22,'b4',2,5 union all
select 11,'c3',6,4


select a.* from @info a,@user b where a.userid=b.userid and b.enddt between '2008-11-01' and '2009-11-01'

水族杰纶 2008-10-27
  • 打赏
  • 举报
回复
select t1.userid,t2.name,t2.[add],t2.code
from talbe1 t1 ,table2 t2 where t1.userid=t2.userid and t1.enddt between '2008-11-01' and '2009-11-01'
dawugui 2008-10-27
  • 打赏
  • 举报
回复
create table [user](userid int, enddt datetime)
insert into [user] values(66 , '2008-08-04')
insert into [user] values(22 , '2008-11-01')
insert into [user] values(11 , '2010-11-01')
create table info(userid int, name varchar(10) , [add] int, code int)
insert into info values(44 , 'a1' , 1 , 2 )
insert into info values(22 , 'b4' , 2 , 5 )
insert into info values(11 , 'c3' , 6 , 4 )
go

select info.* from info , [user] where info.userid = [user].userid and enddt between '2008-11-01' and '2009-11-01'

--drop table [user] , info

/*
userid name add code
----------- ---------- ----------- -----------
22 b4 2 5

(所影响的行数为 1 行)
*/
dawugui 2008-10-27
  • 打赏
  • 举报
回复
select info.* from info , user where info.userid = user.userid and enddt between '2008-11-01' and '2009-11-01'

34,588

社区成员

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

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