200分求一简单sql语句

gzwuyh 2005-05-11 05:54:29
以下语句虽然能达到我所需求,但希望能有简单一点的!
c中的记录是由b1或b2添加的,c的t_id与b1,b2的id关联,但b1和b2没联系,b1,b2中有t_id与a中的id关联,现要统计出c中某条记录在c中出现的次数。虽然下面这样也可以得出,但这样不好对统计的结果进行排序。要排序可以把数据存到数据,然后再一一比较,但这样太麻烦了,还有就是分别把b1,b2加入到一临时表中,但这样会生成一个表。如果只有b1,这我知道可以这样,
select a.name,count(c.id) as ss from b1 right join a on b1.t_id=a.id left c on b1.id=c.t_id group by a.name order by ss desc
*************************************************************
但现在有没有把b1和b2合起来的语句?

<table>
<%set rs=conn.execute("select * from tt")%>
<tr><td><%=rs("name")%>
<%do while not rs.eof%>
<tr><td>
<table>
<tr>
<td>名称</td>
<td>数量</td>
</tr>
<%
'主要在这里

set rs2=conn.execute ("select id,name from a where t_type"=&rs("id"))
do while rs2.eof
sql="select count(*) from c where ( t_id in(select id from b1 where T_id=" rs2("id") & " ) or ( T_id in(select id from b2 where T_id=" rs2("id") &")"
i=conn.execute(sql)

'***********************************************************************
'结束
%>
<tr>
<td><%=rs2("name")%></td>
<td><%=i(0)%></td>
</tr>
<%
rs2.movenext
loop
%>
</table>
</td>
</tr>
<%
rs.movenext
loop
%>
</table>

...全文
109 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzwuyh 2005-05-12
  • 打赏
  • 举报
回复
我自己解决了.
select name,p1.ss+p2.ss ss from
(select a.name,count(c.id) as ss from b1 right join a on b1.t_id=a.id left c on b1.id=c.t_id group by a.name) p1 join (select a.name,count(c.id) as ss from b2 right join a on b2.t_id=a.id left c on b2.id=c.t_id group by a.name) p2 on p1.name=p2.name
order by ss desc
gzwuyh 2005-05-11
  • 打赏
  • 举报
回复
也就是这句话,嵌套查询可以写成
( t_id in(select id from b1 where T_id=" rs2("id") & " ) or ( T_id in(select id from b2 where T_id=" rs2("id") &")
那可不可以把
select a.name,count(c.id) as ss from b1 right join a on b1.t_id=a.id left c on b1.id=c.t_id group by a.name order by ss desc

select a.name,count(c.id) as ss from b2 right join a on b2.t_id=a.id left c on b2.id=c.t_id group by a.name order by ss desc
用or合起来就行了。

gzwuyh 2005-05-11
  • 打赏
  • 举报
回复
再有c中的t_id与是b1,b2中的id,这样union以后id会有重复,对应的b1,b2中的t_id会不一样,即结果会和a中的id不一样。
jarraytan 2005-05-11
  • 打赏
  • 举报
回复
同意前面兄弟。大哥,我看得好头晕啊。。。

帮你顶了!
gzwuyh 2005-05-11
  • 打赏
  • 举报
回复
UNION应该不行吧,b1和b2的字段是完全不一样的。
gzwuyh 2005-05-11
  • 打赏
  • 举报
回复

A字段: A
id name ...............其它
B1字段: B1
id T_id name ..........其它

B1字段: B2
id T_id name ..........其它
C字段: id name t_id ..........其它

即:如a为某单位,b1,b2为不同的类型(b1与b2没有关联),b1,b2的t_id与a的id有关系,c中的记录为b1,b2添加的,c的t_id是b1或b2中id。
现想统计a中某单位在c中的次数


是是非非 2005-05-11
  • 打赏
  • 举报
回复
是不是原本应该是事
a ,b, c三个表关联查询

现在b表被截断b1和b2两个表了


所以,用union all把他们再联回来就OK了
是是非非 2005-05-11
  • 打赏
  • 举报
回复
用UNION ALL 试试

select a.name,count(c.id) as ss from (
select * from b1
union all
select * from b2
)as bb1
right join a on bb1.t_id=a.id left c on bb1.id=c.t_id group by a.name
order by ss desc
aspme 2005-05-11
  • 打赏
  • 举报
回复
汗~~~大哥能说清楚点吗?
ryuginka 2005-05-11
  • 打赏
  • 举报
回复
有空看看,顶
wxf0104 2005-05-11
  • 打赏
  • 举报
回复
我晕,没看明白,郁闷……
帮你顶一顶……

28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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