sql 用法大全

263 阅读1分钟

1.查看表中某一字段是否有重复

select geohash,count(geohash) from geohash_city group by city having count(geohash)>1

2.防止多次写入同一数据 

 insert into 表示插入数据,数据库会检查主键,如果出现重复会报错;  

replace into 表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样;  

insert ignore into 表示,如果中已经存在相同的记录,则忽略当前新数据;

 所以需要一个唯一约束 

 ALTER TABLE `geohash_city` ADD unique(`geohash`);  

唯一约束是通过唯一索引来实现数据的唯一。