我的代码在一个查询中多次使用另一个表中的值更新同一行。如何在一个查询中更新或创建新记录?如果没有带有匹配数据的记录“forum_users_posts_in”,该怎么做呢
userid
idcat
idcustom
create a new record (t.posts = 1)
如果存在这样的记录,则仅更新它(t.posts = t2.sum_total
)
UPDATE #__forum_users_posts_in AS t
JOIN (
SELECT
userid, count(*) AS sum_total
FROM
#__forum_messages AS t2
WHERE
T2.thread = '.$topicId.' and hold = 0
GROUP BY
t2.userid
) AS t2 ON t.userid = t2.userid and (idcat = '.$cat->id.' or idcustom = '.$selectcustom1.')
SET t.posts = t.posts + t2.sum_total
您可以尝试使用此方法来创建或编辑行
INSERT INTO `table` (`columns`) VALUES
(...)
ON DUPLICATE KEY UPDATE
`column` = VALUES (`column`);