mysql数据库,sql语句,把一个表中的统计数量,更新到两一个表中

2024-11-16 03:28:25
推荐回答(3个)
回答1:

update b set b.cnum = (select c.counts from
(SELECT cid,COUNT(*) counts FROM comment GROUP BY cid) c
where a.cid=c.cid);

哎,禁不住说一声,苦逼程序员啊,都这么晚不睡

你试试吧,按照你描述的,我这么写应该就通过了,有问题直接hi

回答2:

Update B
set B.cnum = A.Total
From B
INNER JOIN (SELECT cid,COUNT(*) AS Total FROM A GROUP BY cid) A
ON B.cid =A.cid

回答3:

update b
set b.cnum = a.cnum
From (select cid,count(*) AS cnum from a group by cid) a
where b.cid =a.cid

不知道就说不知道的。还不能跟from。无知真可怕,lz回家吃奶吧