我想要一个解决方案来插入一个记录,如果它不存在所以我在这里搜索并找到了解决方案,但我有另一个问题
INSERT INTO closed_answers (question_ID,subject_ID)SELECT * FROM (SELECT 2,2) AS tmpWHERE NOT EXISTS ( SELECT question_ID FROM closed_answers WHERE question_ID = 2 AND subject_ID = 2) liMIT 1
输出是
#1060 - Duplicate column name '2'
如果我使用任何不相同的2个数字它将起作用,但当2个数字相同时出现问题最佳答案使sql工作的最小变化是为select语句添加别名:
INSERT INTO closed_answers (question_ID,subject_ID)SELECT * FROM (SELECT 2 AS question_ID,2 AS subject_ID) AS tmpWHERE NOT EXISTS ( SELECT question_ID FROM closed_answers WHERE question_ID = 2 AND subject_ID = 2) liMIT 1
但是,如果您对(question_ID,subject_ID)有唯一约束,则可以使用INSERT IGnorE:
INSERT IGnorE INTO closed_answers (question_ID,subject_ID)VALUES (2,2)
总结 以上是内存溢出为你收集整理的mysql – 如果sql中不存在,则插入记录,重复列名全部内容,希望文章能够帮你解决mysql – 如果sql中不存在,则插入记录,重复列名所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)