一个很急的查询的问题!!!!!!!!!!!!!!!!

qiudong_5210 2011-05-27 01:46:57

--想从下面表中查询出对应的某个省有多少人,传入的条件是省份,查询出的人数必须是ISEnable=1的,要用join语句的
tab1
id province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
id city IsEnable pid
1 '朝阳' true 1
2 '浦东' true 2
3 '重庆' false 3
4 '广州' true 4

tab3
id name cid IsEnable
1 'zhangsan' 1 true
2 'lisi' 1 true
3 'wangwu' 3 true
4 'zhaoliu' 2 true
...全文
119 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
sekai2011 2011-05-27
  • 打赏
  • 举报
回复
楼主参照下msdn上的 CTE实现查询返回每个部门经理的各级下属

CROSS APPLY 运算符 这一章,和你需求应该是一样的。。
qiudong_5210 2011-05-27
  • 打赏
  • 举报
回复
一个省下会有多个城市,
一个人名可以关联多个城市,
比如一个业务员会负责北京的朝阳区,东城区,西城区
或者是负责北京上海,广州的情况

所以还需要过滤一下……
qiudong_5210 2011-05-27
  • 打赏
  • 举报
回复
tab3的id和tab4 的id没有任何关系

是通过uname关联的!!!!!!!
qiudong_5210 2011-05-27
  • 打赏
  • 举报
回复
本来应该是100的结果,最后查询出2000多……
hlf1989 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 qiudong_5210 的回复:]
引用 8 楼 hlf1989 的回复:

引用 4 楼 qiudong_5210 的回复:
不好意思错了,表的关系应该是这样的

SQL code

tab1
pid province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
cid ……


都需要判断一下
[/Quote]

select a.id,a.province,人数=count(*)
from tab1 a inner join tab2 b on a.id=b.pid
inner join tab3 c on b.id=c.cid
inner join tab4 d on c.id=d.uid
where c.IsEnable='true' and b.IsEnable='true'
group by a.id,a.province
yanweijie0317 2011-05-27
  • 打赏
  • 举报
回复
select count(t3.id) from tab3 t3
where t3.id in (select t4.uid from tab4 t4 where t4.IsEnable = 1)
and t3.cid in (select t2.cid from tab2 t2 where t2.pid in (select t1.pid from tab1 t1 where t1.province = '北京'));
其实本人认为子查询更好点
yanweijie0317 2011-05-27
  • 打赏
  • 举报
回复
select count(id) from tab3 t3 join tab4 t4 on t4.uid = t3.id join tab2 t2 on t3.cid = t2.cid join tab1 t1 on t2.pid = t1.pid and t4.IsEnable = 1 and t1.province = '北京';
亲手测试的sql语句
快溜 2011-05-27
  • 打赏
  • 举报
回复
select a.id,a.province,人数=count(*)
from tab1 a inner join tab2 b on a.id=b.pid
inner join tab3 c on b.id=c.cid
inner join tab4 d on c.id=d.uid
where c.IsEnable='true' and a.id=@id--参数
group by a.id,a.province
yanweijie0317 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 yanweijie0317 的回复:]

引用 4 楼 qiudong_5210 的回复:

不好意思错了,表的关系应该是这样的
SQL code

tab1
pid province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
cid ……
[/Quote]
没看清楚
yanweijie0317 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qiudong_5210 的回复:]

不好意思错了,表的关系应该是这样的
SQL code

tab1
pid province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
cid city IsEnable pid
1 '朝阳' ……
[/Quote]
tab4和之前的表有什么关系么?
qiudong_5210 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hlf1989 的回复:]

引用 4 楼 qiudong_5210 的回复:
不好意思错了,表的关系应该是这样的

SQL code

tab1
pid province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
cid ……
[/Quote]

都需要判断一下
hlf1989 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qiudong_5210 的回复:]
不好意思错了,表的关系应该是这样的

SQL code

tab1
pid province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
cid city IsEnable pid
1 ……
[/Quote]
楼主说的IsEnable字段在两个表里都有,楼主要指明是那一个
qiudong_5210 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fredrickhu 的回复:]

引用 4 楼 qiudong_5210 的回复:
不好意思错了,表的关系应该是这样的

SQL code

tab1
pid province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
cid ……
[/Quote]

真不好意思,我这里网速也挺慢的,加载个网页都得等一会……
--小F-- 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qiudong_5210 的回复:]
不好意思错了,表的关系应该是这样的

SQL code

tab1
pid province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
cid city IsEnable pid
1 ……
[/Quote]

...网速慢 刚写好
--小F-- 2011-05-27
  • 打赏
  • 举报
回复
select
a.id,a.province,人数=count(1)
from
tab1 a inner join tab2 b on a.id=b.pid
join
tab3 c on b.id=c.cid
where
c.IsEnable='true'
qiudong_5210 2011-05-27
  • 打赏
  • 举报
回复
不好意思错了,表的关系应该是这样的

tab1
pid province IsEnable
1 '北京' true
2 '上海' true
3 '重庆' true
4 '广州' true

tab2
cid city IsEnable pid
1 '朝阳' true 1
2 '浦东' true 2
3 '重庆' false 3
4 '广州' true 4

tab3
id cid uname
1 1 'zhangsan'
2 1 'lisi'
3 3 'wangwu'
4 2 'zhaoliu'

tab4
uid uname IsEnable
1 'zhangsan' true
2 'lisi' true
3 'wangwu' true
4 'zhaoliu' true
ForFumm 2011-05-27
  • 打赏
  • 举报
回复
SELECT * FROM tab3 A LEFT JOIN tab2 B ON A.cid=B.id LEFT JOIN tab1 C ON B.pid=C.id
WHERE C.province= @yourpara AND A.isEnable=1
快溜 2011-05-27
  • 打赏
  • 举报
回复
select a.id,a.province,人数=count(*)
from tab1 a inner join tab2 b on a.id=b.pid
inner join tab3 c on b.id=c.cid
where c.IsEnable='true' and a.id=@id--参数
group by a.id,a.province
快溜 2011-05-27
  • 打赏
  • 举报
回复
select a.id,a.province,人数=count(*)
from tab1 a inner join tab2 b on a.id=b.pid
inner join tab3 c on b.id=c.cid
where c.IsEnable='true'

22,210

社区成员

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

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