{"id":46,"date":"2015-05-30T06:56:48","date_gmt":"2015-05-30T06:56:48","guid":{"rendered":"http:\/\/www.josejavierfm.es\/blog\/?p=46"},"modified":"2015-06-19T06:57:11","modified_gmt":"2015-06-19T06:57:11","slug":"borrar-registros-mysql-a-partir-de-una-select-en-la-misma-tabla","status":"publish","type":"post","link":"https:\/\/www.josejavierfm.es\/blog\/2015\/05\/30\/borrar-registros-mysql-a-partir-de-una-select-en-la-misma-tabla\/","title":{"rendered":"borrar registros mysql a partir de una select en la misma tabla"},"content":{"rendered":"<p>Si intentamos borrar registros de una tabla a partir de una select<\/p>\n<p>delete from customers where customers.customerNumber in<br \/>\n(<br \/>\nselect customers.customerNumber<br \/>\nfrom customers<br \/>\nleft join payments on customers.customerNumber=payments.customerNumber<br \/>\ngroup by customers.customerNumber<br \/>\nhaving count(payments.amount)=0<br \/>\n)<\/p>\n<p>obtenemos el error:<br \/>\n<strong>[Err] 1093<\/strong> &#8211; You can&#8217;t specify target table &#8216;customers&#8217; for update in FROM clause<\/p>\n<p>Existen dos maneras de borrar registros de una tabla a partir de una select de la misma tabla:<\/p>\n<p><strong>usando in<\/strong><\/p>\n<p>delete from customers where customers.customerNumber in<br \/>\n(select customerNumber from (<br \/>\nselect customers.customerNumber<br \/>\nfrom customers<br \/>\nleft join payments on customers.customerNumber=payments.customerNumber<br \/>\ngroup by customers.customerNumber<br \/>\nhaving count(payments.amount)=0<br \/>\n) as t )<\/p>\n<p><strong>y con inner join<\/strong><\/p>\n<p>delete <em style=\"color: #ff0000;\">customers<\/em> from customers inner join<br \/>\n(select customerNumber from (<br \/>\nselect customers.customerNumber<br \/>\nfrom customers<br \/>\nleft join payments on customers.customerNumber=payments.customerNumber<br \/>\ngroup by customers.customerNumber<br \/>\nhaving count(payments.amount)=0<br \/>\n) as t ) tt on customers.customerNumber=tt.customerNumber<\/p>\n<p><em style=\"color: #ff0000;\">es importante decir que tabla borras, pues al hacer el join la sentencia no sabria cual borrar.<\/em><\/p>\n<p>&nbsp;<\/p>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=josejavierfm\" class=\"twitter-share-button\">Twittear<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Si intentamos borrar registros de una tabla a partir de una select delete from customers where customers.customerNumber in ( select customers.customerNumber from customers left join payments on customers.customerNumber=payments.customerNumber group by customers.customerNumber having count(payments.amount)=0 ) obtenemos el error: [Err] 1093 &#8211; You can&#8217;t specify target table &#8216;customers&#8217; for update in FROM clause Existen dos maneras de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/46"}],"collection":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/comments?post=46"}],"version-history":[{"count":1,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/posts\/46\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.josejavierfm.es\/blog\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}