gpt4 book ai didi

php - 将一个 MySQL 表中的列插入或更新到另一个错误

转载 作者:行者123 更新时间:2023-11-29 21:05:23 25 4
gpt4 key购买 nike

我有一个表behaviour,我在其中计算每个哈希的页面。我想将结果传输到新表new_table中。如果 new_table 上的哈希存在,则只需更新页数。否则,如果它是在 behaviour 中发现的新 hash,但尚未插入 new_table,只需将其添加即可。

INSERT INTO new_table (hash, pages)
SELECT hash, COUNT( id ) AS pages
FROM behaviour
ON DUPLICATE KEY UPDATE new_table.pages=behaviour.pages
GROUP BY hash

我在第 5 行“按哈希分组”时收到错误。我做错了什么?

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY hash' at line 5

最佳答案

使用以下查询。这会起作用

INSERT INTO new_table (hash, pages)
SELECT hash, COUNT( id ) AS pages
FROM behaviour GROUP BY hash
ON DUPLICATE KEY UPDATE pages=VALUES(pages)

您必须在重复键检查之前添加 group by 子句

关于php - 将一个 MySQL 表中的列插入或更新到另一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36860204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com