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 requests ('user_id','subject','text','time') VALUES (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:
- 不插入任何东西
- 更新时间"和用户 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
这篇关于在插入之前检查重复的插入语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在插入之前检查重复的插入语句
基础教程推荐
- Sql Server 字符串到日期的转换 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01