sql语句如下,判断条件是本表和另外一张表的ID要相等。
请问该如何实现
UPDATE `writeback` SET `WB_CONTENT_NUM` =
(
SELECT COUNT(`writeback`.`WB_CONTENT`)
FROM `writeback`,releasediscussionquestions
WHERE writeback.`RDQ_ID` = releasediscussionquestions.`RDQ_ID`
)
WHERE writeback.`RDQ_ID` = releasediscussionquestions.`RDQ_ID`;
2 个解决方案
#1
试试这样
UPDATE writeback
SET WB_CONTENT_NUM = ( SELECT COUNT(1)
FROM releasediscussionquestions
WHERE writeback.RDQ_ID = releasediscussionquestions.RDQ_ID
);
#2
mysql 的话,试下这种
UPDATE `writeback` a INNER JOIN (SELECT RDQ_ID,COUNT(`WB_CONTENT`) AS WB_CONTENT_NUM FROM `writeback`,releasediscussionquestions) b ON a.`RDQ_ID` = b.`RDQ_ID` SET a.`WB_CONTENT_NUM` = b.WB_CONTENT_NUM
UPDATE `writeback` a INNER JOIN (SELECT RDQ_ID,COUNT(`WB_CONTENT`) AS WB_CONTENT_NUM FROM `writeback`,releasediscussionquestions) b ON a.`RDQ_ID` = b.`RDQ_ID` SET a.`WB_CONTENT_NUM` = b.WB_CONTENT_NUM
#1
试试这样
UPDATE writeback
SET WB_CONTENT_NUM = ( SELECT COUNT(1)
FROM releasediscussionquestions
WHERE writeback.RDQ_ID = releasediscussionquestions.RDQ_ID
);
#2
mysql 的话,试下这种
UPDATE `writeback` a INNER JOIN (SELECT RDQ_ID,COUNT(`WB_CONTENT`) AS WB_CONTENT_NUM FROM `writeback`,releasediscussionquestions) b ON a.`RDQ_ID` = b.`RDQ_ID` SET a.`WB_CONTENT_NUM` = b.WB_CONTENT_NUM
UPDATE `writeback` a INNER JOIN (SELECT RDQ_ID,COUNT(`WB_CONTENT`) AS WB_CONTENT_NUM FROM `writeback`,releasediscussionquestions) b ON a.`RDQ_ID` = b.`RDQ_ID` SET a.`WB_CONTENT_NUM` = b.WB_CONTENT_NUM