gpt4 book ai didi

mysql - 在 REPLACE 上增加字段值

转载 作者:可可西里 更新时间:2023-11-01 06:27:40 25 4
gpt4 key购买 nike

我之前使用 UPDATE 语句成功完成了此操作,但没有使用 REPLACE。

当用户结帐时,我将最喜欢的项目保存在 mysql 表中。

最喜欢的表是:

USER (int)
ITEM (int)
COUNT (int default 0)

我正在尝试的 SQL 是:

REPLACE INTO favs (user,item,count) VALUES ('1','3', count + 1)

虽然它不会抛出任何错误,但它似乎也不会增加值。

这可能吗?谢谢。

最佳答案

看起来它在替换时不能像这样工作。来自manual :

You cannot refer to values from the current row and use them in the new row. If you use an assignment such as SET col_name = col_name + 1, the reference to the column name on the right hand side is treated as DEFAULT(col_name), so the assignment is equivalent to SET col_name = DEFAULT(col_name) + 1.

编辑:

但是,INSERT ... ON DUPLICATE UPDATE 可能会完成您想要完成的任务:

INSERT INTO favs (user, item) VALUES (2, 3) 
ON DUPLICATE KEY UPDATE count = count + 1;

关于mysql - 在 REPLACE 上增加字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4153250/

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