{"id":1824,"date":"2005-07-29T16:06:39","date_gmt":"2005-07-29T15:06:39","guid":{"rendered":"https:\/\/yvespeneveyre8e29bf869d.wordpress.com\/2005\/07\/29\/sql-efficace-efficient-sql\/"},"modified":"2005-07-29T16:06:39","modified_gmt":"2005-07-29T15:06:39","slug":"sql-efficace-efficient-sql","status":"publish","type":"post","link":"https:\/\/www.peneveyre.com\/en\/2005\/07\/29\/sql-efficace-efficient-sql\/","title":{"rendered":"SQL Efficace &#8211; Efficient SQL"},"content":{"rendered":"<p><P>Au d??tour d&#8217;une rencontre avec un guru du SQL, j&#8217;ai pu admirer le code suivant, permettant d&#8217;effacer tous le doublon d&#8217;une table. Imaginons une table (aTable) avec une seule colonne (aNumber), aucune cl??, mais avec plusieurs lignes contenant la m??me valeur :<\/P><br \/>\n<P>1<BR>1<BR>2<BR>1<BR>3<BR>3<BR>3<BR>1<BR>2<BR>1<\/P><br \/>\n<P>Le but est du supprimer tous les doublons, ceci en une seule requ??te. Vous avez la r??ponse ?<\/P><br \/>\n<P>Voici le code SQL :<\/P><br \/>\n<P><SPAN><SPAN>set<\/SPAN> rowcount 1<BR><BR><SPAN>select<\/SPAN> 1<BR><SPAN>while<\/SPAN> <SPAN>@@rowcount<\/SPAN> &gt; 0<BR><SPAN>delete<\/SPAN> test <BR><SPAN>where<\/SPAN> 1 &lt; (<SPAN>select<\/SPAN> <SPAN>count<\/SPAN>(*) <SPAN>from<\/SPAN> test t2 <SPAN>where<\/SPAN> test.id = t2.id)<BR><BR><SPAN>set<\/SPAN> rowcount 0<BR><\/SPAN><\/P><br \/>\n<P>&nbsp;<\/P><br \/>\n<P>&nbsp;<\/P><br \/>\n<P><EM>Some couple of weeks ago, I had the opportunity to meet SQL guru, and I have seen a very nice SQL code, deleting duplicates rows in a table. Let&#8217;s imagine a table (aTable) with only one field (aNumber), without any key but multiple rows with the same value :<\/EM><\/P><br \/>\n<P><EM>1<BR>1<BR>2<BR>1<BR>3<BR>3<BR>3<BR>1<BR>2<BR>1<\/EM><\/P><br \/>\n<P><EM>The goal is to delete duplicate values in only one SQL request. You have the answed ?<\/EM><\/P><br \/>\n<P><EM>Here is the code :<\/EM><\/P><br \/>\n<P><SPAN><SPAN>set<\/SPAN> rowcount 1<BR><BR><SPAN>select<\/SPAN> 1<BR><SPAN>while<\/SPAN> <SPAN>@@rowcount<\/SPAN> &gt; 0<BR><SPAN>delete<\/SPAN> test <BR><SPAN>where<\/SPAN> 1 &lt; (<SPAN>select<\/SPAN> <SPAN>count<\/SPAN>(*) <SPAN>from<\/SPAN> test t2 <SPAN>where<\/SPAN> test.id = t2.id)<BR><BR><SPAN>set<\/SPAN> rowcount 0<\/SPAN><SPAN><BR><\/SPAN><SPAN><\/P><\/SPAN><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Un exemple de code SQL vraiment tr??s beau ?? voir. An example of a very nice SQL code.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1824","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/posts\/1824","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/comments?post=1824"}],"version-history":[{"count":0,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/posts\/1824\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/media?parent=1824"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/categories?post=1824"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peneveyre.com\/en\/wp-json\/wp\/v2\/tags?post=1824"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}