多表查询问题。在线及时结帐
后台调用多个表 表a 表 b 表 c 表 d
在前台的一个表格里面显示
a表 一共多少条 认证多少条
b表 一共多少条 认证多少条
c表 一共多少条 认证多少条
d表 一共多少条 认证多少条
求思路和例子,在线关注,及时结帐。。。。
[解决办法]
这几个表有关联么?
有的话用inner join
如果表结构都一样就用 union 连接
[解决办法]
join
[解决办法]
提供个思路
写个存储过程,然后按照有多少个表就查询多次这个表得到相关的统计信息,然后组成一个临时表返回
[解决办法]
这样写也行。
select aa.xxx,bb.xxx,cc.xxx,dd.xxx from a aa,b bb,c cc,d dd where aa.user_name=bb.user_name and aa.user_name=cc.user_name and aa.user_name=dd.user_name and 加上你的查询条件。
不知道你的user_name是怎么对应的,where条件还可以这样
where aa.user_name=bb.user_name and bb.user_name=cc.user_name and cc.user_name=dd.user_name and .............
[解决办法]
select count(*) from a aa,b bb,c cc,d dd where aa.user_name=bb.user_name and aa.user_name=cc.user_name and aa.user_name=dd.user_name and ......
或些个存储过程调用
[解决办法]
不会写存储过程就重复执行调用吧。
sql1语句
"select count(*) from a aa,b bb,c cc,d dd where aa.user_name=bb.user_name and aa.user_name=cc.user_name and aa.user_name=dd.user_name and aa.aa_session = " + session + " and bb.bb_session = " + session + " and cc.cc_session = " + session + " and dd.dd_session = " + session;
sql2语句
"select count(renzheng) from a aa,b bb,c cc,d dd where aa.user_name=bb.user_name and aa.user_name=cc.user_name and aa.user_name=dd.user_name and aa.aa_session = " + session + " and bb.bb_session = " + session + " and cc.cc_session = " + session + " and dd.dd_session = " + session;
[解决办法]
哦,这样啊,上面的不对
[解决办法]
既然是四个独立的表, 肯定要独立查询四次啦,谁让你不放在一个表里面的
[解决办法]
同意 Yzw_2006()
用union
[解决办法]
select *
into #tempT
from a as aa (nolock)
join b as bb (nolock)
on aa.username = bb.username
join c as cc (nolock)
on bb.username = cc.username
join d as dd (nolock)
on cc.username = dd.username
where dd.username = @username
//**then you can calculate the value you want***
select count(1) from #tempT
i don 't know the condition you count for, so please add it yourself,i think this quality will be better if you user temp table.