插入或更新
INSERT INTO or Update
我的代码在一个查询中使用另一个 table 的值多次更新同一行。如何在一次查询中更新或创建新记录?如果没有匹配数据
的记录 "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`);
我的代码在一个查询中使用另一个 table 的值多次更新同一行。如何在一次查询中更新或创建新记录?如果没有匹配数据
的记录 "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`);