MySQL的經(jīng)典用法(一)----數(shù)據(jù)重復(fù)判斷文章分類:數(shù)據(jù)庫 本人工作中的實際應(yīng)用.在采集數(shù)據(jù)的時候,要求數(shù)據(jù)采集以后,不能重復(fù).同時也要求有多個實例同時運轉(zhuǎn),保證數(shù)據(jù)采集的連續(xù)性.因此總結(jié)了一下,做成了如下的小試驗.核心代碼如下:
表結(jié)構(gòu):只有3個字段 id,name,password
CREATE TABLE `tt` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `password` varchar(255) DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; sql語句 1)推薦
insert ignore into tt(name,password) values('phl','123')
2)不推薦,因為insert的時候select,會鎖定select的表
insert into tt(name,password) select 'phl','123' from dual where not exists(select * from tt where name='phl' and password='123') 這個SQL語句的含義是,如果插入的數(shù)據(jù) name='phl',password='123'不存在,則執(zhí)行插入; |
|
|