举例:查询“retail_sales_dataset”表中是否存在重复数据该表共9个字段,由于该方法需要把所有字段名称都列出来,可用“SHOW COLUMNS FROM retail_sales_dataset;”来输出所有字段名称,然后选中字段名复制粘贴即可
方法一:如返回结果为0则无重复数据,文末附上操作视频
SELECT count(*) FROM (
SELECT `Transaction ID`,Date,`Customer ID`,Gender,Age,
`Product Category`,Quantity,`Price per Unit`,`Total Amount`,count(*)as records FROM
retail_sales_dataset GROUP BY 1,2,3,4,5,6,7,8,9
)a WHERE records>1;
输出结果:
方法二:如records>1则列出所有重复内容,如不满足条件则为空
SELECT `Transaction ID`,Date,`Customer ID`,Gender,Age,
`Product Category`,Quantity,`Price per Unit`,`Total Amount`,count(*)as records
FROM
retail_sales_dataset
GROUP BY 1,2,3,4,5,6,7,8,9
HAVING count(*)>1;
输出结果
02使用distinct去重
-- 查询结果去重
SELECT DISTINCT * FROM retail_sales_dataset;
-- 查询去重后的客户ID
SELECT DISTINCT `Customer ID` FROM retail_sales_dataset;
-- 使用join时添加DISTINCT去重,也可以使用其他方法去重
SELECT DISTINCT a.id,a.name
FROM table1 a
join table2 b on a.id=b.id;