国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

mysql查詢篩選重復(fù)數(shù)據(jù)sql語句

2024-07-24 12:37:14
字體:
供稿:網(wǎng)友

以前講過大量的重復(fù)數(shù)據(jù)過濾語句,下面小編來給大家介紹一些自己收藏了查詢篩選重復(fù)數(shù)據(jù)sql語句,希望對各位朋友有所幫助.

查詢重復(fù)數(shù)據(jù)數(shù)量,代碼如下:

select device_id from device group by device_id having count(device_id) > 1;

查詢所有重復(fù)數(shù)據(jù),代碼如下:

  1. select userid, device_id, create_date from device where device_id in (select device_id from device group by device_id having count(device_id) > 1) order by device_id,create_date desc ; 

重復(fù)一條中create_date 最新的那一條,代碼如下:

select max(create_date) from device group by device_id having count(device_id)>1;

篩選查詢,代碼如下:

  1. select * from device where device_id in (select device_id from device group by device_id having count(device_id) > 1) and create_date not in (select max(create_date) from device group by device_id having count(device_id)>1) order by device_id,create_date desc ; 

下面再看一些實(shí)例吧.

表結(jié)構(gòu)如下,代碼如下:

  1. mysql> desc test1; 
  2. +--------------+------------------+------+-----+---------+----------------+ 
  3. | Field        | Type             | Null | Key | Default | Extra          | 
  4. +--------------+------------------+------+-----+---------+----------------+ 
  5. | ID           | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
  6. | SENDERNAME   | varchar(32)      | YES  |     | NULL    |                | 
  7. | RECEIVERNAME | varchar(64)      | YES  |     | NULL    |                | 
  8. | SENDER       | varchar(64)      | NO   |     | NULL    |                | 
  9. | RECEIVER     | varchar(64)      | NO   |     | NULL    |                | 
  10. | SUBJECT      | varchar(512)     | NO   |     | NULL    |                | 
  11. | CONTENT      | text             | NO   |     | NULL    |                | 
  12. | PRIORITY     | int(11)          | NO   | MUL | NULL    |                | 
  13. | STATUS       | int(11)          | NO   | MUL | NULL    |                | 
  14. | CREATETIME   | datetime         | NO   |     | NULL    |                | 
  15. | SENDTIME     | datetime         | YES  |     | NULL    |                | 
  16. +--------------+------------------+------+-----+---------+----------------+ 

subject和RECEIVER,需要做uniq key,但設(shè)計(jì)時(shí)未做,,后面的數(shù)據(jù)就有很多重復(fù)的記錄.

1.查詢需要?jiǎng)h除的記錄,會保留一條記錄,代碼如下:

  1. select a.id,a.subject,a.RECEIVER from test1 a left join (select c.subject,c.RECEIVER ,max(c.id) as  bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b on a.id< b.bid where  a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid 

2.刪除重復(fù)記錄,只保留一條記錄,注意,subject,RECEIVER 要索引,否則會很慢的,代碼如下:

  1. delete a from test1 a, (select c.subject,c.RECEIVER ,max(c.id) as  bid from test1 c where status=0 GROUP BY RECEIVER,SUBJECT having count(1) >1) b where a.subject=b.subject and a.RECEIVER = b.RECEIVER and a.id < b.bid;  //Vevb.com 

好了篩選重復(fù)數(shù)據(jù)的sql語句大概就這些了,如果你能理解那幾乎不擔(dān)心重復(fù)數(shù)據(jù)這一說了.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 雷山县| 自治县| 会理县| 孙吴县| 台江县| 大理市| 锦州市| 鹤岗市| 万山特区| 鹤庆县| 潜山县| 安乡县| 长岭县| 霞浦县| 连云港市| 嵊州市| 保靖县| 荆州市| 焦作市| 孟州市| 北京市| 平乡县| 东莞市| 象山县| 阿坝县| 盐城市| 墨脱县| 桑日县| 库尔勒市| 东明县| 渝中区| 措美县| 广安市| 静宁县| 青铜峡市| 盐山县| 巴中市| 林西县| 温泉县| 桐乡市| 黑水县|