您的位置:首页 > 数据库

SQL 查找重复记录

2013-07-27 10:29 369 查看
SQL 查找重复记录
USE TEST

GO

IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME = 'product')

DROP TABLE product

CREATE TABLE product

(

 ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,

 Pid INT NOT NULL,

 Pname VARCHAR(50) NOT NULL,

 Punit CHAR(10) NOT NULL,

 Pspec VARCHAR(50),

 PbarCode VARCHAR(20),

)

INSERT INTO product(Pid,Pname,Punit,Pspec,PbarCode) VALUES(10000,'欧莱雅日间修复','瓶','50ML','1975126589653')

INSERT INTO product(Pid,Pname,Punit,Pspec,PbarCode) VALUES(10001,'欧莱雅夜间修复','瓶','50ML','1975126589643')

INSERT INTO product(Pid,Pname,Punit,Pspec,PbarCode) VALUES(10002,'倩碧','盒','150ML','1545126589653')

INSERT INTO product(Pid,Pname,Punit,Pspec,PbarCode) VALUES(10003,'百分百','瓶','250ML','2575126589653')

INSERT INTO product(Pid,Pname,Punit,Pspec,PbarCode) VALUES(10004,'欧本洗面脸','瓶','80ML','1275126589653')

INSERT INTO product(Pid,Pname,Punit,Pspec,PbarCode) VALUES(10005,'艾迪达斯','瓶','40ML','1975126589653')

INSERT INTO product(Pid,Pname,Punit,Pspec,PbarCode) VALUES(10006,'SK2','瓶','20ML','1975126589653')

 

--查某一列(或多列)的重复值(只能查出重复记录的值,不能整个记录的信息)

--如:查找barCode 重复的记录

select PbarCode from product

group by PbarCode

having(count(*))>1

--查找重复条形码商品

select * from product

where PbarCode in (

select PbarCode from product

group by PbarCode

having(count(*))>1

)

 --查找重复条形码商品二,跟上面的结果是一样的  by 李全

select * from product where PbarCode in

(select PbarCode from 

(select p1.PbarCode,count(p1.PbarCode) number from product p1,(select distinct(PbarCode) from product) p2 where

p1.PbarCode=p2.PbarCode group by p1.PbarCode) a1 where number>1)

--将表重命名

sp_rename 'product1','product'

原网址:http://blog.csdn.net/eastlift/article/details/1456984

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: