字段列表中的“field”列在mysql中不明确

dffbzjpn  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(267)
INSERT INTO  users (phone, no_of_coupon) 
SELECT couponentries.phone, 1
FROM couponentries LEFT JOIN users ON couponentries.phone = 
users.phone
WHERE users.phone IS NULL
ON DUPLICATE KEY UPDATE users.no_of_coupon = users.no_of_coupon + 1;

我试图在couponentries表中输入日期,然后它将自动只在users表中插入电话号码。如果有重复的电话号码,则优惠券的号码不会增加1。但是当我在phpmyadmin触发器中执行这段代码时,下面有一个错误。有人能帮帮我吗?谢谢
无法执行列“users.no\u of \u coupon”在字段列表中不明确

vlf7wbxs

vlf7wbxs1#

您需要为join子句中使用的users表提供别名

INSERT INTO  users (phone, no_of_coupon) 
SELECT c.phone, 1
FROM couponentries c 
LEFT JOIN users u ON c.phone = u.phone
WHERE u.phone IS NULL
ON DUPLICATE KEY UPDATE users.no_of_coupon = users.no_of_coupon + 1;

我猜你不需要连接部分,你可以直接使用

INSERT INTO users (phone) 
SELECT c.phone
FROM couponentries c 
ON DUPLICATE KEY UPDATE no_of_coupon = no_of_coupon + 1;

演示

相关问题