用于在插入之前检查重复项的插入语句 [英] Insert statement that checks for duplicate before insert
问题描述
我需要进行插入,但前提是不存在类似的记录
例如:
I need to make a insert but only if similar record don't exists
for example:
INSERT INTO请求('user_id','subject','text','time')值(56,'test','test 1234',6516516)
INSERT INTO requests ('user_id','subject','text','time') VALUES (56,'test','test 1234',6516516)
但要检查另一个记录中是否存在相同的主题"和文本":
but to check if there are same 'subject' and 'text' in another record to:
- 不插入任何内容
- 更新时间"和"user_id"
在这两种情况下我都需要sql,因为目前我不确定我将使用什么.
预先感谢!
I need sql for both cases because I'm no sure at this moment what I'm going to use.
Thanks in advance!
推荐答案
INSERT INTO requests ('user_id','subject','text','time')
VALUES (56,'test','test 1234',6516516)
ON DUPLICATE KEY UPDATE time = VALUES(time), user_id = VALUES(user_id)
已将相关列设置为索引UNIQUE.
Have the relevant columns set to index UNIQUE.
这将插入一行,但是如果主题或文本(或两者)已经存在,则改为使用给定的time
和user_id
This will insert a row, but if subject or text (or both) already exist, you instead update the existing row with given time
and user_id
这篇关于用于在插入之前检查重复项的插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!