Mysql IF NOT EXISTS then 语法
我现在无法根据我的需要生成一个有效的 mysql 查询。我已经接近
IF NOT EXISTS(SELECT *
FROM Users
WHERE Downloads='c6310e0ae33f9377f6ae7994bbafe6e20113aaf4')
UPDATE Users
SET `Downloads`='c6310e0ae33f9377f6ae7994bbafe6e20113aaf4'
WHERE `User`='andrewfree';
“This failed”,我正在阅读有关 INSERT IGNORE INTO 的内容,但这也没有帮助。我想要做的就是将此哈希添加到用户下载字段(如果任何人下载字段的表中不存在该哈希)。
I can't manage to produce a working mysql query right now for what I want. I got as close as
IF NOT EXISTS(SELECT *
FROM Users
WHERE Downloads='c6310e0ae33f9377f6ae7994bbafe6e20113aaf4')
UPDATE Users
SET `Downloads`='c6310e0ae33f9377f6ae7994bbafe6e20113aaf4'
WHERE `User`='andrewfree';
This fails, I was reading about INSERT IGNORE INTO but that hasn't helped either. What I am trying to do is add this hash to a users Download field if it doesn't exist in the table in anyones Download field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
移动
WHERE
子句中的EXISTS
:或者在 Users.Downloads 上添加唯一键并使用 UPDATE IGNORE:
如果此
Downloads
值,更新将不会完成。请参阅更新语法。
Move the
EXISTS
in theWHERE
clause:Or add a unique key on Users.Downloads and use UPDATE IGNORE:
If a row already exists with this
Downloads
value, the UPDATE will not be done.See UPDATE syntax.
编辑:
实际上,上面的语句会抛出错误。我认为这可以做到(未经测试)
EDIT:
Actually, the above statement will throw an error. I think this will do it (untested)