1. 程式人生 > >mysql查詢某一個或幾個欄位重複值是哪個,重複幾條

mysql查詢某一個或幾個欄位重複值是哪個,重複幾條

select 列名1,count(1) as count 
from 表名
group by  列名1
having count>1  and 其他條件

 

select 列名1,列名2,count(1) as count 
from 表名
group by  列名1,列名2 
having count>1  and 其他條件
 

原理:先按照要查詢出現重複資料的列,進行分組查詢。count>1代表出現2次或2次以上。

示例:

/*查詢重複資料*/
select serialnum,cdate,count(*) as count 
from m_8_customer_temp_20180820bak 
group by serialnum,cdate having  count>1 and cdate>='2018-08-20 00:00:00';