高分求一个多表统计的SQL,来者有分,有答案马上结贴

ABCatai 2008-12-25 06:38:31
现有3张表table1, table2,table3,每个表中分别有保存user_id的字段fld01, fld01, fld03, 并且

每张表中都有created字段,现在要统计在某一时间段内这三张表中保存的user的个数(其中同一个用

户只能统计一次,不能累加),如:
在2008-12-24到2008-12-25这两天:
table1保存的user id有:1,2,3
table2保存的user id有: 2,4,5
talbe3保存的user id有:7
则统计出的不重复user id有:1,2,3,4,5,7共6个,所以返回结果应该是6

请问各位老大这个SQL怎么写啊?
...全文
180 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sky-Yang 2008-12-26
  • 打赏
  • 举报
回复

select count(distinct user_id)
from table1,table2,table3
where created between 'time1' and 'time2'

union
的话效率高一点
懒虎 2008-12-26
  • 打赏
  • 举报
回复
select count(distinct user_id) from
select fld01 user_id from table1 where created between '2008-12-24' and '2008-12-25'
union
select fld02 user_id from table2 where created between '2008-12-24' and '2008-12-25'
union
select fld03 user_id from table3 where created between '2008-12-24' and '2008-12-25'
)

这个应该是正确的
jiang_jiajia10 2008-12-26
  • 打赏
  • 举报
回复
UP
kakanote 2008-12-26
  • 打赏
  • 举报
回复
Ssafsdfsfd
joynet007 2008-12-26
  • 打赏
  • 举报
回复

select count(user_id) from(
select fld01 user_id from table1 where created between '2008-12-24' and '2008-12-25'
union
select fld02 user_id from table2 where created between '2008-12-24' and '2008-12-25'
union
select fld03 user_id from table3 where created between '2008-12-24' and '2008-12-25'
)
就可以了!
我不是大明 2008-12-26
  • 打赏
  • 举报
回复
一楼是对的,CSDN高手多,
weir75034 2008-12-26
  • 打赏
  • 举报
回复
呵呵,没分了吧
yupengt 2008-12-25
  • 打赏
  • 举报
回复
向高手致敬
SylvanLiu 2008-12-25
  • 打赏
  • 举报
回复
用union就不用distinct;
union all需要用distinct
lnfszl 2008-12-25
  • 打赏
  • 举报
回复
一楼说的很好啊,怎么不结帖啊
凌霄野鹤 2008-12-25
  • 打赏
  • 举报
回复
接分
possibleonline 2008-12-25
  • 打赏
  • 举报
回复
学习
socool627 2008-12-25
  • 打赏
  • 举报
回复
来晚了 。。。
guolimin1118 2008-12-25
  • 打赏
  • 举报
回复

union就是可以过滤重复id
当然也可以用distinct
来去掉重复字段
spring_sun1 2008-12-25
  • 打赏
  • 举报
回复
来晚了。。。。
zh2208 2008-12-25
  • 打赏
  • 举报
回复
select count(distinct user_id) from
select fld01 user_id from table1 where created between '2008-12-24' and '2008-12-25'
union
select fld02 user_id from table2 where created between '2008-12-24' and '2008-12-25'
union
select fld03 user_id from table3 where created between '2008-12-24' and '2008-12-25'
)

正解
sunnylyy 2008-12-25
  • 打赏
  • 举报
回复
楼上,用了union就不用distinct了。union all则需要用distinct
hbwhwang 2008-12-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ljking2006 的回复:]
select count(user_id) from(
select fld01 user_id from table1 where created between '2008-12-24' and '2008-12-25'
union
select fld02 user_id from table2 where created between '2008-12-24' and '2008-12-25'
union
select fld03 user_id from table3 where created between '2008-12-24' and '2008-12-25'
)
[/Quote]
加个DISTINCT就对了
select count(distinct user_id) from
select fld01 user_id from table1 where created between '2008-12-24' and '2008-12-25'
union
select fld02 user_id from table2 where created between '2008-12-24' and '2008-12-25'
union
select fld03 user_id from table3 where created between '2008-12-24' and '2008-12-25'
)
sll853 2008-12-25
  • 打赏
  • 举报
回复
学习ing
ljking2006 2008-12-25
  • 打赏
  • 举报
回复
select count(user_id) from(
select fld01 user_id from table1 where created between '2008-12-24' and '2008-12-25'
union
select fld02 user_id from table2 where created between '2008-12-24' and '2008-12-25'
union
select fld03 user_id from table3 where created between '2008-12-24' and '2008-12-25'
)
加载更多回复(5)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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