gpt4 book ai didi

mysql - INSERT INTO...ON DUPLICATE KEY UPDATE 增量语法

转载 作者:行者123 更新时间:2023-11-29 01:56:39 32 4
gpt4 key购买 nike

我正在尝试将一个值从一个表递增到另一个表,但我无法找出正确的语法。有人可以帮我形成正确的陈述吗?提前致谢:

当前语句:

INSERT INTO points_1_day (nick, amount) 
(SELECT nick, SUM(amount) as increment
FROM points_log
WHERE dt >= NOW()-INTERVAL 1 day GROUP BY nick)
ON DUPLICATE KEY UPDATE
points_1_day.amount=points_1_day.amount+points_log.increment;

我收到此错误消息:

ERROR 1054 (42S22): Unknown column 'points_log.increment' in 'field list'

最佳答案

ON UPDATE 子句中的表达式不能引用 SELECT 中的列。

但他们可以引用 VALUES(amount) 来获取您尝试插入给定行的值。

INSERT INTO points_1_day (nick, amount) 
(SELECT nick, SUM(amount) as increment
FROM points_log
WHERE dt >= NOW()-INTERVAL 1 day GROUP BY nick)
ON DUPLICATE KEY UPDATE
points_1_day.amount=points_1_day.amount+VALUES(amount);

让我测试一下,看看它是否有效...

mysql> insert into points_1_day values (123, 10);
Query OK, 1 row affected (0.01 sec)

mysql> insert into points_log (nick, amount, dt) values (123, 15, NOW());
Query OK, 1 row affected (0.13 sec)

mysql> select * from points_1_day;
+------+--------+
| nick | amount |
+------+--------+
| 123 | 10 |
+------+--------+
1 row in set (0.00 sec)

mysql> INSERT INTO points_1_day (nick, amount) (SELECT nick, SUM(amount) as increment
FROM points_log WHERE dt >= NOW()-INTERVAL 1 day GROUP BY nick)
ON DUPLICATE KEY UPDATE points_1_day.amount=points_1_day.amount+values(amount);
Query OK, 2 rows affected (0.02 sec)
Records: 1 Duplicates: 1 Warnings: 0

mysql> select * from points_1_day;
+------+--------+
| nick | amount |
+------+--------+
| 123 | 25 |
+------+--------+
1 row in set (0.00 sec)

mysql> INSERT INTO points_1_day (nick, amount) (SELECT nick, SUM(amount) as increment
FROM points_log WHERE dt >= NOW()-INTERVAL 1 day GROUP BY nick)
ON DUPLICATE KEY UPDATE points_1_day.amount=points_1_day.amount+values(amount);
Query OK, 2 rows affected (0.00 sec)
Records: 1 Duplicates: 1 Warnings: 0

mysql> select * from points_1_day;
+------+--------+
| nick | amount |
+------+--------+
| 123 | 40 |
+------+--------+

是的,它似乎有效。


解释:http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html说:

You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement. In other words, VALUES(col_name) in the ON DUPLICATE KEY UPDATE clause refers to the value of col_name that would be inserted, had no duplicate-key conflict occurred. This function is especially useful in multiple-row inserts. The VALUES() function is meaningful only in INSERT ... UPDATE statements and returns NULL otherwise. Example:

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);

这也适用于 INSERT...SELECT。但是 VALUES() 的参数是您要插入的列名,而不是 SELECT 中相应列的名称。

关于mysql - INSERT INTO...ON DUPLICATE KEY UPDATE 增量语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27111968/

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