title: MySQL Group where any 3 of 5 columns match
body: <p>I am searching an addresses table for duplicates, using SOUNDEX to find the duplicates. This works fine, and it requires all 5 soundex columns to match in order to group</p>
  
  <p>However, I want to GROUP where ANY 3 of my 5 SOUNDEX columns match.</p>
  
  <p>Here is my current query:</p>
  
  <pre><code>SELECT `Address`.`id`, 
      SOUNDEX(`Address`.`address_company_name`) as soundex_address_company_name,
      SOUNDEX(`Address`.`contact_name`) as soundex_contact_name,
      SOUNDEX(`Address`.`street_address`) as soundex_street_address,
      SOUNDEX(`Address`.`suburb`) as soundex_suburb,
      SOUNDEX(`Address`.`city`) as soundex_city,
      `Address`.`address_country_id`,
      `Address`.`address_zone_id`,
      `Address`.`postcode`, 
      COUNT(*)
  FROM 
      `addresses` AS `Address` 
  WHERE 
       ((`Address`.`address_company_name` IS NOT NULL) 
       OR (`Address`.`contact_name` IS NOT NULL))  
  GROUP BY 
      SOUNDEX(address_company_name),
      SOUNDEX(contact_name),
      SOUNDEX(street_address),
      SOUNDEX(suburb),
      SOUNDEX(city),
      address_country_id,
      address_zone_id,
      postcode
  HAVING
      COUNT(*) &gt; 1
  </code></pre>
  
  <p>I understand how to do this with multiple queries, ie: loop through each address in our database and then re-query the database for addresses which match any 3 of the 5 columns, however I am hoping to do this in fewer queries as the above query executes very quickly.</p>
  
  <p>I also understand that were this possible, some records may be grouped multiple times, I don't mind if this is the case but I am unsure whether this flies in the face of MySQL logic?</p>