且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL插入查询以防止重复数据

更新时间:2023-01-22 18:11:32

您好,

您可以使用 EXISTS 检查此链接 MSDN [ ^ ]

示例 [ ^ ]

***的问候

M.Mitwalli
Hi ,
You can use EXISTS Check this link MSDN[^]
Example [^]
Best Regards
M.Mitwalli


我会建议您在表格中使用主键(或设置唯一约束)。

这样可以防止重复输入。



如何将UNIQUE约束应用于现有SQL列 [ ^ ]可能会帮助您入门。
I would recommend you have a primary key (or setup a unique constraint) in your table.
This would prevent duplicate entry.

How to apply UNIQUE constraints to existing SQL columns[^] might help you get started.


因为您没有

1)SQL的状态类型( MySQL,Oracle,SQL Server ....)

2)举例说明你尝试过的事情

我会给你一个SQL Server的提示。查看 LEFT JOIN

Since you did not
1) State type of SQL (MySQL, Oracle, SQL Server....)
2) Give example of what you have tried
I will give you a hint for SQL Server. Look at LEFT JOIN
INSERT INTO Table2 (variable list)
SELECT variable list 
FROM Table1 
LEFT JOIN Table2 
ON Table1.keyfield = Table2.keyfield 
WHERE Table2.keyfield IS NULL;