sql语句问题:更新表的问题

maggie_ma 2005-07-19 10:15:35
例如有两个表:table1, table2


table2表的结构是id,tid,address

table1表的结构是tid,num


我要做的是select num=count(*),tid from table2 group by tid

将得到的相对应的tid,更新table1表的相应的num值
update table1 set num = .....where tid = ....

例如记录: table1 表
tid num
1 null
2 null

table2表
id, tid, address
1 1 192.121.0.1
2 1 192.121.0.2
3 2 192.155.0.1


结果: talbe1表
tid num
1 2
2 1


请教sql语句怎么写





...全文
93 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
godhuang 2005-07-19
  • 打赏
  • 举报
回复
select tid,count(*) as num into T# from table1 group by tid
insert into table2 select * from T#
drop table T#
javaCoffee33 2005-07-19
  • 打赏
  • 举报
回复
update table1 set num = (select count(*) from table2 where table1.tid = table2.tid )

先试试把

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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