查看可用如下方法:
1、创建测试表,插入数据:
create table product(id int,name varchar(10),totol int) insert into product values (1,'香蕉',100)insert into product values (2,'橘子',67)insert into product values (3,'葡萄',89)insert into product values (4,'苹果',235)insert into product values (5,'香蕉',77)insert into product values (6,'芒果',34)insert into product values (7,'葡萄',78)insert into product values (8,'梨',24)表中数据如:
2、如果查询name列有重复的数据,可执行sql语句:
select from product where name in (select name from product group by name having COUNT()>1)
说明:查询的结果就是香蕉和葡萄在表中是有重复的,要把香蕉和葡萄的所有记录都查询出来,结果如图:
法一: 用Group by语句 此查找很快的select count(num), max(name) from student –查找表中num列重复的,列出重复的记录数,并列出他的name属性group by numhaving count(num) >;1 –按num分组后找出表中num列重复,即出现次数大于一次delete from student(上面Select的)这样的话就把所有重复的都删除了。—–慎重法二:当表比较大(例如10万条以上)时,这个方法的效率之差令人无法忍受,需要另想办法:—- 执行下面SQL语句后就可以显示所有DRAWING和DSNO相同且重复的记录SELECT FROM EM5_PIPE_PREFABWHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D –D相当于First,SecondWHERE EM5_PIPE_PREFABDRAWING=DDRAWING ANDEM5_PIPE_PREFABDSNO=DDSNO);—- 执行下面SQL语句后就可以删除所有DRAWING和DSNO相同且重复的记录DELETE FROM EM5_PIPE_PREFABWHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB DWHERE EM5_PIPE_PREFABDRAWING=DDRAWING ANDEM5_PIPE_PREFABDSNO=DDSNO);法一: 用Group by语句 此查找很快的select count(num), max(name) from student –查找表中num列重复的,列出重复的记录数,并列出他的name属性group by numhaving count(num) >;1 –按num分组后找出表中num列重复,即出现次数大于一次delete from student(上面Select的)这样的话就把所有重复的都删除了。—–慎重法二:当表比较大(例如10万条以上)时,这个方法的效率之差令人无法忍受,需要另想办法:—- 执行下面SQL语句后就可以显示所有DRAWING和DSNO相同且重复的记录SELECT FROM EM5_PIPE_PREFABWHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D –D相当于First,SecondWHERE EM5_PIPE_PREFABDRAWING=DDRAWING ANDEM5_PIPE_PREFABDSNO=DDSNO);—- 执行下面SQL语句后就可以删除所有DRAWING和DSNO相同且重复的记录DELETE FROM EM5_PIPE_PREFABWHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB DWHERE EM5_PIPE_PREFABDRAWING=DDRAWING ANDEM5_PIPE_PREFABDSNO=DDSNO);
以上就是关于怎么防止数据库中插入重复数据全部的内容,包括:怎么防止数据库中插入重复数据、如何避免向数据库插入重复记录、查看数据库是否有重复字段的sql语句等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)