title: PHP/ mySQL joins - muliple duplicated rows in result
body: <p>In a wordpress based booking system I am trying to find the number of Adults that are going to all events for all time - this info is stored in the <strong>wp_woocommerce_order_itemmeta</strong> table (e.g. <em>meta_key</em>='Adults', <em>meta_value</em>='6') . In phpmyadmin I can see in the <strong>wp_woocommerce_order_itemmeta</strong> table there are 20 rows which have a <em>meta_key</em> value of 'Adults' (ie 20 total bookings). But when I run this code rather than printing 20 lines, each line gets repeated approx 42 times, so i end up with 840 lines (or so)</p>
  
  <p>e.g.</p>
  
  <p>meta_key Adults
  meta_value 6
  meta_key Adults
  meta_value 6
  meta_key Adults
  meta_value 6
  meta_key Adults
  meta_value 6
  meta_key Adults
  meta_value 6
  meta_key Adults
  meta_value 6
  meta_key Adul .. and so on</p>
  
  <p>rather than:</p>
  
  <p>meta_key Adults
  meta_value 6
  meta_key Adults
  meta_value 8
  meta_key Adults
  meta_value 11
  meta_key Adults
  meta_value 1
  meta_key Adults
  meta_value 1
  meta_key Adults
  meta_value 2 .. and so on up to the desired 20 pairs of results</p>
  
  <p>This is the code:</p>
  
  <pre><code>global $wpdb;
  $results= $wpdb-&gt;get_results( '
  select wp_woocommerce_order_itemmeta.meta_key, wp_woocommerce_order_itemmeta.meta_value
  FROM wp_postmeta
  inner JOIN wp_posts
  ON wp_postmeta.post_id = wp_posts.ID
  inner JOIN wp_woocommerce_order_items
  ON wp_woocommerce_order_items.order_id = wp_posts.ID
  inner JOIN wp_woocommerce_order_itemmeta
  ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
  where 1=1
  AND wp_woocommerce_order_itemmeta.meta_key ="Adults"
  ', OBJECT );
  foreach ($results as $res2) {
  foreach ($res2 as $key =&gt; $value) {
              echo "$key $value\n";
              }
  }
  </code></pre>
  
  <p>If I add DISTINCT to the SELECT then I lose rows where the number of adults going is the same, so that's not what I want. I just want to print those 20 rows that exist then I can add up the number of Adults going to all events for all time.</p>
  
  <p>I need the joins in there as later I will first have to check that a booking is complete (which is in the wp_posts table), and I need to relate the post ID to the order_ID to relate that, etc etc</p>
  
  <p>Where is my logic failing at this early stage? Thanks for any help.</p>