求一SQL查询语句 急 在线 谢谢!

lhb2000 2003-08-30 03:23:13
表1 表2 表3 表4 表5 表6
id username userid 日期 数据 ...
表2 3 4 5 6 结构相同 但内容不同 不能合并
现在要查询一日期 查出表1中的所有用户 在表 2 3 4 5 6 的查询日期中有无数据 显示如下

用户 表2 表3 表4 表5 表6
aaa 有 有 有 有 有
bbb 有 无 有 无 无
.....
应该用left join来写吧 但具体sql查询如何写呢?
...全文
28 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhb2000 2003-08-30
  • 打赏
  • 举报
回复
谢谢各位 成功了
wozhuchuanwei 2003-08-30
  • 打赏
  • 举报
回复
select a.username
,表2=case when b.userid is not null then '有' else '无' end
,表3=case when c.userid is not null then '有' else '无' end
,表4=case when d.userid is not null then '有' else '无' end
,表5=case when e.userid is not null then '有' else '无' end
,表6=case when f.userid is not null then '有' else '无' end
from 表1 a
left join (select userid from 表2 where DateDiff(day,日期,@d)=0) b
on a.id=b.userid
left join (select userid from 表3 where DateDiff(day,日期,@d)=0) c
on a.id=c.userid
left join (select userid from 表4 where DateDiff(day,日期,@d)=0) d
on a.id=d.userid
left join (select userid from 表5 where DateDiff(day,日期,@d)=0) e
on a.id=e.userid
left join (select userid from 表6 where DateDiff(day,日期,@d)=0) f
on a.id=f.userid
jiezhi 2003-08-30
  • 打赏
  • 举报
回复
小改一下:
select a.username
,表2=case when b.userid is not null then '有' else '无' end
,表3=case when c.userid is not null then '有' else '无' end
,表4=case when d.userid is not null then '有' else '无' end
,表5=case when e.userid is not null then '有' else '无' end
,表6=case when f.userid is not null then '有' else '无' end
from 表1 a
left join (select userid from 表2 where 日期=@d) b on a.id=b.userid
left join (select userid from 表3 where 日期=@d) c on a.id=c.userid
left join (select userid from 表4 where 日期=@d) d on a.id=d.userid
left join (select userid from 表5 where 日期=@d) e on a.id=e.userid
left join (select userid from 表6 where 日期=@d) f on a.id=f.userid

否則會出現?號
txlicenhe 2003-08-30
  • 打赏
  • 举报
回复
Select username as 用户,
IsNull((Select top 1 '有' from 表2 where userid = a.userid),'无') as 表2,
IsNull((Select top 1 '有' from 表3 where userid = a.userid),'无') as 表3,
IsNull((Select top 1 '有' from 表4 where userid = a.userid),'无') as 表4,
IsNull((Select top 1 '有' from 表5 where userid = a.userid),'无') as 表5,
IsNull((Select top 1 '有' from 表6 where userid = a.userid),'无') as 表6
from 表1 a


zjcxc 2003-08-30
  • 打赏
  • 举报
回复
--或:
declare @d datedime
set @d=getdate() --要查询的日期

select username
,表2=case when exists(select 1 from 表2 where 日期=@d and userid=a.id) then '有' else '无' end
,表3=case when exists(select 1 from 表3 where 日期=@d and userid=a.id) then '有' else '无' end
,表4=case when exists(select 1 from 表4 where 日期=@d and userid=a.id) then '有' else '无' end
,表5=case when exists(select 1 from 表5 where 日期=@d and userid=a.id) then '有' else '无' end
,表5=case when exists(select 1 from 表6 where 日期=@d and userid=a.id) then '有' else '无' end
from 表1 a
zjcxc 2003-08-30
  • 打赏
  • 举报
回复
declare @d datedime
set @d=getdate() --要查询的日期

select a.username
,表2=case when b.userid is null then '无' else '有' end
,表3=case when c.userid is null then '无' else '有' end
,表4=case when d.userid is null then '无' else '有' end
,表5=case when e.userid is null then '无' else '有' end
,表6=case when f.userid is null then '无' else '有' end
from 表1 a
left join (select userid from 表2 where 日期=@d) b on a.id=b.userid
left join (select userid from 表3 where 日期=@d) c on a.id=c.userid
left join (select userid from 表4 where 日期=@d) d on a.id=d.userid
left join (select userid from 表5 where 日期=@d) e on a.id=e.userid
left join (select userid from 表6 where 日期=@d) f on a.id=f.userid

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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