gpt4 book ai didi

mysql - 重复 key 更新时插入...从...中选择

转载 作者:行者123 更新时间:2023-11-29 15:38:00 25 4
gpt4 key购买 nike

我正在执行插入查询,如果唯一键已存在,则大多数列都需要更新为新值。事情是这样的:

INSERT INTO lee(exp_id, created_by, 
location, animal,
starttime, endtime, entct,
inact, inadur, inadist,
smlct, smldur, smldist,
larct, lardur, lardist,
emptyct, emptydur)
SELECT id, uid, t.location, t.animal, t.starttime, t.endtime, t.entct,
t.inact, t.inadur, t.inadist,
t.smlct, t.smldur, t.smldist,
t.larct, t.lardur, t.lardist,
t.emptyct, t.emptydur
FROM tmp t WHERE uid=x
ON DUPLICATE KEY UPDATE ...;
//update all fields to values from SELECT,
// except for exp_id, created_by, location, animal,
// starttime, endtime

我不确定 UPDATE 子句的语法应该是什么。如何从 SELECT 子句引用当前行?

最佳答案

MySQL 将假设 equals 之前的部分引用 INSERT INTO 子句中指定的列,第二部分引用 SELECT 列。

INSERT INTO lee(exp_id, created_by, location, animal, starttime, endtime, entct, 
inact, inadur, inadist,
smlct, smldur, smldist,
larct, lardur, lardist,
emptyct, emptydur)
SELECT id, uid, t.location, t.animal, t.starttime, t.endtime, t.entct,
t.inact, t.inadur, t.inadist,
t.smlct, t.smldur, t.smldist,
t.larct, t.lardur, t.lardist,
t.emptyct, t.emptydur
FROM tmp t WHERE uid=x
ON DUPLICATE KEY UPDATE entct=t.entct, inact=t.inact, ...

关于mysql - 重复 key 更新时插入...从...中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58024672/

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