title: SQL query based on two columns
body: <p>I have a quite large products table and I need some smart solution to find out which products are (in woocommerce terms) Simple, Variable, and Variation.</p>
  <p>In the example table below, ids 6 and 7 are Simple products (they don't have children), 1 and 4 are Variable products (they have children), and 2,3,5 are Variations (they are children).</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>item_id</th>
  <th>parent_id</th>
  <th>other_columns</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>aaa</td>
  <td>aaa</td>
  <td>...</td>
  </tr>
  <tr>
  <td>2</td>
  <td>aaa-1</td>
  <td>aaa</td>
  <td>...</td>
  </tr>
  <tr>
  <td>3</td>
  <td>aaa-2</td>
  <td>aaa</td>
  <td>...</td>
  </tr>
  <tr>
  <td>4</td>
  <td>bbb</td>
  <td>bbb</td>
  <td>...</td>
  </tr>
  <tr>
  <td>5</td>
  <td>bbb-1</td>
  <td>bbb</td>
  <td>...</td>
  </tr>
  <tr>
  <td>6</td>
  <td>ccc</td>
  <td>ccc</td>
  <td>...</td>
  </tr>
  <tr>
  <td>7</td>
  <td>ddd</td>
  <td>ddd</td>
  <td>...</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>My goal is to first get Simple products, then get Variable products and import them and lastly get Variations and import them to the existing parent Variable products.</p>
  <p>I have a solution for Simple products:</p>
  <pre><code>SELECT *
  FROM (
      SELECT *
      FROM $table_name
      WHERE item_id = parent_id
      GROUP BY parent_id
      HAVING COUNT(*) = 1
  ) AS ONLY_ONCE
  </code></pre>
  <p>I've tried similar for the Variable products (<code>count(*) &gt; 1</code>), but it doesn't work...</p>
  <p>For Variation I have:</p>
  <pre><code>SELECT * 
  FROM $table_name 
  WHERE item_id != parent_id
  </code></pre>
  <ol>
  <li>Simple products - select all where item_id is equal with parent_id, and parent_id occurs only once.</li>
  <li>Variable products - select all where item_id is equal with parent_id and parent_id occurs more than once.</li>
  <li>Variation - select all where item_id and parent_id are not equal.</li>
  </ol>
  <p>How could I write those three queries, especially the second one? any help is appreciated.</p>