MySql插入不重複紀錄 不依據主鍵判斷

2021-08-22 11:41:03 字數 503 閱讀 5408

語法格式: insert … select … where not exist

例如: insert into student(name) select '張三' from dual where not exists (select name from student where name = '張三')

這種方法其實就是使用了mysql的乙個臨時表的方式,根據子查詢的條件判斷是否插入,但是裡面使用到了子查詢,效率也會有一點點影響,如果可以使用主鍵判斷重複,建議還是使用.insert ignore into或者on duplicate key update這種方式。

這種方式的dual是mysql定義的特殊虛擬表,主要為select.... from....這種寫法而提供的。如果寫select * from `dual`; 和 show create table `dual`檢視表結構都會報錯的,因為他本身是虛擬表,是不支援這樣查詢的。

mysql 計算非重複 MySQL 不重複插入

sql unique 約束 unique 約束唯一標識資料庫表中的每條記錄。unique 和 primary key 約束均為列或列集合提供了唯一性的保證。primary key 擁有自動定義的 unique 約束。請注意,每個表可以有多個 unique 約束,但是每個表只能有乙個 primary ...

mysql批量插入不重複資料

1.設定唯一建 前提是 有唯一建可用,but基本很難有這樣的場景。datum有唯一建。create table datum id int 11 notnull auto increment mid int 11 notnull default 0 rong liang varchar 255 col...

MySQL插入 更新資料時,要求不重複

一 插入資料時 當插入資料時,要求資料表的某一列 比如name 不重複,語法如下 insert into table field1,field2,fieldn select field1 field2 fieldn from dual where notexists select field fro...