Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL delete except one
    primarykey
    data
    text
    <p>I've the following database structure:</p> <pre><code>id idproperty idgbs 1 1 136 2 1 128 3 1 10 4 1 1 5 2 136 6 2 128 7 2 10 8 2 1 9 3 561 10 3 560 11 3 10 12 3 1 13 4 561 14 4 560 15 4 10 16 4 1 17 5 234 18 5 120 19 5 1 20 6 234 21 6 120 22 6 1 </code></pre> <p>Here are the details:</p> <p>The table refers <code>idproperty</code> with different geographic location. For example: </p> <pre><code>idgbs 1 refers to United States 10 refers to Alabama with parentid 1 (United States) 128 refers to Alabama Gulf Coast with parentid 10 (Alabama) 136 Dauphin Island with parentid 128 (Alabama Gulf Coast) </code></pre> <p>So, the structure is:</p> <pre><code>United States &gt; Alabama &gt; Alabama Gulf Coast &gt; Dauphin Island </code></pre> <p>I want to delete all entries for idproperty EXCEPT the first with the set of idgbs 136, 128, 10, 1 i.e. leave atleast 1 property in all GBS and delete others.</p> <p>Also, sometimes it is 4 level of geographic entries, sometimes it is 3 level.</p> <p>Please share the logic &amp; SQL query to delete all entries except one in every unique GBS.</p> <p>GBS 1, 10, 128, 136 is one unique, so database should only contain 1 property id with these GBS.</p> <p>After the query, the table would look like this:</p> <pre><code>id idproperty idgbs 1 1 136 2 1 128 3 1 10 4 1 1 9 3 561 10 3 560 11 3 10 12 3 1 17 5 234 18 5 120 19 5 1 </code></pre> <p>Rephrasing the question:</p> <p>I want to keep properties in every root level GBS i.e. there should be only ONE property in Dauphin Island. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload