{"id":334,"date":"2010-08-20T09:20:59","date_gmt":"2010-08-20T01:20:59","guid":{"rendered":"http:\/\/elon.852128.com\/blog\/?p=334"},"modified":"2010-08-20T09:20:59","modified_gmt":"2010-08-20T01:20:59","slug":"mysql%e6%9f%a5%e8%af%a2%e5%8f%8a%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d%e8%ae%b0%e5%bd%95%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/elon.852128.com\/blog\/2010\/08\/20\/mysql%e6%9f%a5%e8%af%a2%e5%8f%8a%e5%88%a0%e9%99%a4%e9%87%8d%e5%a4%8d%e8%ae%b0%e5%bd%95%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"MySQL\u67e5\u8be2\u53ca\u5220\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>\u67e5\u8be2\u53ca\u5220\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5<br \/>\n(\u4e00)<br \/>\n1\u3001\u67e5\u627e\u8868\u4e2d\u591a\u4f59\u7684\u91cd\u590d\u8bb0\u5f55\uff0c\u91cd\u590d\u8bb0\u5f55\u662f\u6839\u636e\u5355\u4e2a\u5b57\u6bb5\uff08peopleId\uff09\u6765\u5224\u65ad<br \/>\nselect * from people<br \/>\nwhere peopleId in (select  peopleId  from  people  group  by  peopleId  having  count(peopleId) > 1)<\/p>\n<p>2\u3001\u5220\u9664\u8868\u4e2d\u591a\u4f59\u7684\u91cd\u590d\u8bb0\u5f55\uff0c\u91cd\u590d\u8bb0\u5f55\u662f\u6839\u636e\u5355\u4e2a\u5b57\u6bb5\uff08peopleId\uff09\u6765\u5224\u65ad\uff0c\u53ea\u7559\u6709rowid\u6700\u5c0f\u7684\u8bb0\u5f55<br \/>\ndelete from people<br \/>\nwhere peopleId  in (select  peopleId  from people  group  by  peopleId   having  count(peopleId) > 1)<br \/>\nand rowid not in (select min(rowid) from  people  group by peopleId  having count(peopleId )>1)<\/p>\n<p>3\u3001\u67e5\u627e\u8868\u4e2d\u591a\u4f59\u7684\u91cd\u590d\u8bb0\u5f55\uff08\u591a\u4e2a\u5b57\u6bb5\uff09<br \/>\nselect * from vitae a<br \/>\nwhere (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq  having count(*) > 1)<\/p>\n<p>4\u3001\u5220\u9664\u8868\u4e2d\u591a\u4f59\u7684\u91cd\u590d\u8bb0\u5f55\uff08\u591a\u4e2a\u5b57\u6bb5\uff09\uff0c\u53ea\u7559\u6709rowid\u6700\u5c0f\u7684\u8bb0\u5f55<br \/>\ndelete from vitae a<br \/>\nwhere (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)<br \/>\nand rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)<\/p>\n<p>5\u3001\u67e5\u627e\u8868\u4e2d\u591a\u4f59\u7684\u91cd\u590d\u8bb0\u5f55\uff08\u591a\u4e2a\u5b57\u6bb5\uff09\uff0c\u4e0d\u5305\u542browid\u6700\u5c0f\u7684\u8bb0\u5f55<br \/>\nselect * from vitae a<br \/>\nwhere (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)<br \/>\nand rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)<\/p>\n<p>(\u4e8c)<br \/>\n\u6bd4\u65b9\u8bf4<br \/>\n\u5728A\u8868\u4e2d\u5b58\u5728\u4e00\u4e2a\u5b57\u6bb5\u201cname\u201d\uff0c<br \/>\n\u800c\u4e14\u4e0d\u540c\u8bb0\u5f55\u4e4b\u95f4\u7684\u201cname\u201d\u503c\u6709\u53ef\u80fd\u4f1a\u76f8\u540c\uff0c<br \/>\n\u73b0\u5728\u5c31\u662f\u9700\u8981\u67e5\u8be2\u51fa\u5728\u8be5\u8868\u4e2d\u7684\u5404\u8bb0\u5f55\u4e4b\u95f4\uff0c\u201cname\u201d\u503c\u5b58\u5728\u91cd\u590d\u7684\u9879\uff1b<br \/>\nSelect Name,Count(*) From A Group By Name Having Count(*) > 1<\/p>\n<p>\u5982\u679c\u8fd8\u67e5\u6027\u522b\u4e5f\u76f8\u540c\u5927\u5219\u5982\u4e0b:<br \/>\nSelect Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1<\/p>\n<p>(\u4e09)<br \/>\n\u65b9\u6cd5\u4e00<\/p>\n<p>declare @max integer,@id integer<\/p>\n<p>declare cur_rows cursor local for select \u4e3b\u5b57\u6bb5,count(*) from \u8868\u540d group by \u4e3b\u5b57\u6bb5 having count(*) >\uff1b 1<\/p>\n<p>open cur_rows<\/p>\n<p>fetch cur_rows into @id,@max<\/p>\n<p>while @@fetch_status=0<\/p>\n<p>begin<\/p>\n<p>select @max = @max -1<\/p>\n<p>set rowcount @max<\/p>\n<p>delete from \u8868\u540d where \u4e3b\u5b57\u6bb5 = @id<\/p>\n<p>fetch cur_rows into @id,@max<\/p>\n<p>end<\/p>\n<p>close cur_rows<\/p>\n<p>set rowcount 0<\/p>\n<p>\u3000\u3000\u65b9\u6cd5\u4e8c<\/p>\n<p>\u3000\u3000\u6709\u4e24\u4e2a\u610f\u4e49\u4e0a\u7684\u91cd\u590d\u8bb0\u5f55\uff0c\u4e00\u662f\u5b8c\u5168\u91cd\u590d\u7684\u8bb0\u5f55\uff0c\u4e5f\u5373\u6240\u6709\u5b57\u6bb5\u5747\u91cd\u590d\u7684\u8bb0\u5f55\uff0c\u4e8c\u662f\u90e8\u5206\u5173\u952e\u5b57\u6bb5\u91cd\u590d\u7684\u8bb0\u5f55\uff0c\u6bd4\u5982Name\u5b57\u6bb5\u91cd\u590d\uff0c\u800c\u5176\u4ed6\u5b57\u6bb5\u4e0d\u4e00\u5b9a\u91cd\u590d\u6216\u90fd\u91cd\u590d\u53ef\u4ee5\u5ffd\u7565\u3002<\/p>\n<p>\u3000\u30001\u3001\u5bf9\u4e8e\u7b2c\u4e00\u79cd\u91cd\u590d\uff0c\u6bd4\u8f83\u5bb9\u6613\u89e3\u51b3\uff0c\u4f7f\u7528<\/p>\n<p>select distinct * from tableName<\/p>\n<p>\u3000\u3000\u5c31\u53ef\u4ee5\u5f97\u5230\u65e0\u91cd\u590d\u8bb0\u5f55\u7684\u7ed3\u679c\u96c6\u3002<\/p>\n<p>\u3000\u3000\u5982\u679c\u8be5\u8868\u9700\u8981\u5220\u9664\u91cd\u590d\u7684\u8bb0\u5f55\uff08\u91cd\u590d\u8bb0\u5f55\u4fdd\u75591\u6761\uff09\uff0c\u53ef\u4ee5\u6309\u4ee5\u4e0b\u65b9\u6cd5\u5220\u9664<\/p>\n<p>select distinct * into #Tmp from tableName<\/p>\n<p>drop table tableName<\/p>\n<p>select * into tableName from #Tmp<\/p>\n<p>drop table #Tmp<\/p>\n<p>\u3000\u3000\u53d1\u751f\u8fd9\u79cd\u91cd\u590d\u7684\u539f\u56e0\u662f\u8868\u8bbe\u8ba1\u4e0d\u5468\u4ea7\u751f\u7684\uff0c\u589e\u52a0\u552f\u4e00\u7d22\u5f15\u5217\u5373\u53ef\u89e3\u51b3\u3002<\/p>\n<p>\u3000\u30002\u3001\u8fd9\u7c7b\u91cd\u590d\u95ee\u9898\u901a\u5e38\u8981\u6c42\u4fdd\u7559\u91cd\u590d\u8bb0\u5f55\u4e2d\u7684\u7b2c\u4e00\u6761\u8bb0\u5f55\uff0c\u64cd\u4f5c\u65b9\u6cd5\u5982\u4e0b<\/p>\n<p>\u3000\u3000\u5047\u8bbe\u6709\u91cd\u590d\u7684\u5b57\u6bb5\u4e3aName,Address\uff0c\u8981\u6c42\u5f97\u5230\u8fd9\u4e24\u4e2a\u5b57\u6bb5\u552f\u4e00\u7684\u7ed3\u679c\u96c6<\/p>\n<p>select identity(int,1,1) as autoID, * into #Tmp from tableName<\/p>\n<p>select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID<\/p>\n<p>select * from #Tmp where autoID in(select autoID from #tmp2)<\/p>\n<p>\u3000\u3000\u6700\u540e\u4e00\u4e2aselect\u5373\u5f97\u5230\u4e86Name\uff0cAddress\u4e0d\u91cd\u590d\u7684\u7ed3\u679c\u96c6\uff08\u4f46\u591a\u4e86\u4e00\u4e2aautoID\u5b57\u6bb5\uff0c\u5b9e\u9645\u5199\u65f6\u53ef\u4ee5\u5199\u5728select\u5b50\u53e5\u4e2d\u7701\u53bb\u6b64\u5217\uff09<\/p>\n<p>(\u56db)<br \/>\n\u67e5\u8be2\u91cd\u590d<\/p>\n<p>select * from tablename where id in (select id from tablename group by id having count(id) > 1<br \/>\n)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u8be2\u53ca\u5220\u9664\u91cd\u590d\u8bb0\u5f55\u7684\u65b9\u6cd5 (\u4e00) 1\u3001\u67e5\u627e\u8868\u4e2d\u591a\u4f59\u7684\u91cd\u590d\u8bb0\u5f55\uff0c\u91cd\u590d&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":1,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":335,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions\/335"}],"wp:attachment":[{"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elon.852128.com\/blog\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}