我的代码使用另一个表中的值在一个查询中多次更新同一行。如何在一个查询中更新或创建新记录?如果没有记录“ 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
答案 0 :(得分:0)
您可以尝试使用此方法来创建或编辑行
INSERT INTO `table` (`columns`) VALUES
(...)
ON DUPLICATE KEY UPDATE
`column` = VALUES (`column`);