求一条sql语句,大家帮忙

landog 2003-05-08 02:31:33
res_types表

TID Name Memo
1 n1 m1
2 n2 m2

resources 表

RID TID
1 1
2 1
3 2

如何查到所有res_types中的数据,同时查到该条数据TID相同的resources中记录个数
比如得到
TID Name Memo count()
1 n1 m1 2
2 n2 m2 1


3ks
...全文
47 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fyg_02971 2003-05-08
  • 打赏
  • 举报
回复
create table res_type (tid int,name varchar(20),memo varchar(20))
create table resources (rid int ,tid int)

insert res_type values(1,'n1','m1')
insert res_type values(2,'n2','m2')

insert resources values(1,1)
insert resources values(2,1)
insert resources values(3,2)

select res.*,(select count(tid) from resources where tid = res.tid) from res_type res
ybz3721 2003-05-08
  • 打赏
  • 举报
回复
select a.tid tid,a.name name,a.memo memo,count(b.rid) count
from res_types a,resources b
where a.tid=b.tid
group by a.tid,a.name,a.memo

(所影响的行数为 2 行)


已测试。。。


CrazyFor 2003-05-08
  • 打赏
  • 举报
回复
select *, (select count(*) from resources where TID = A.TID) as count from res_types as A
yoki 2003-05-08
  • 打赏
  • 举报
回复

create table res_types(TID int, Name varchar(20), Memo varchar(200))
insert into res_types values(1, 'n1', 'm1')
insert into res_types values(2, 'n2', 'm2')

create table resources(RID int,TID int)
insert into resources values( 1 , 1)
insert into resources values( 2 , 1)
insert into resources values( 3 , 2)

select a.*,(select count(tid) from resources where tid=a.tid) as 数目 from res_types a

结果:
-----------------------------*/
TID Name Memo 数目
--- ----- -----
1 n1 m1 2
2 n2 m2 1
firetoucher 2003-05-08
  • 打赏
  • 举报
回复
select a.TID, Name, Memo, c
from
res_types a,
(select TID, count(*) c from resources group by TID) b
where a.TID = b.TID
dapper 2003-05-08
  • 打赏
  • 举报
回复
select a.tid,a.name,a.memo,b.tid_count from res_types a left join (select TID ,count(TID) as TID_Count from res_types group by tid) b on a.tid=b.tid

忘了分组了。呵呵。
dapper 2003-05-08
  • 打赏
  • 举报
回复
select a.tid,a.name,a.memo,b.tid_count from res_types a left join (select TID ,count(TID) as TID_Count from res_types ) b on a.tid=b.tid
happydreamer 2003-05-08
  • 打赏
  • 举报
回复

select a.*,b.count1 as count()
from res_types a
join
(select tid,count(rid) as count1 from resources group by tid)b
on a.tid=b.tid
愉快的登山者 2003-05-08
  • 打赏
  • 举报
回复
select *, (select count(*) from resources where TID = A.TID) from res_types as A
landog 2003-05-08
  • 打赏
  • 举报
回复
对亚,俺想知道一条语句可以实现不?
ralpher2017 2003-05-08
  • 打赏
  • 举报
回复
用游标和临时表可以实现,不过我想应该还有其他的方法能得到,gz中....

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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