Page 1 of 1

Compare sets

Posted: Thu Feb 14, 2008 2:26 pm
by GeXus
I'm having a hell of a time trying to figure out how to do this...

I have a table that as a column called 'country_codes', this is a comma delimited list of country codes (i.e., US,UK,CA), If I want to look up a record based on a country code I can use
find_in_set('US', country_codes)
But, what I would like to do now is fine ANY records from multiple sets, such as find_in_set('US,CA', country_codes), and if any of the two first values are found, return the record.

Any ideas?

Re: Compare sets

Posted: Fri Feb 15, 2008 1:19 pm
by John Cartwright
You'll need to post the php code for the find_in_set function, although it will come down to modifying your query to accept a string such as 'US,CA' or an array('US', 'CA') and passing it to mysql's IN() function.

Code: Select all

function find_in_set(array $needle) 
{
   $sql = "SELECT ....  WHERE `countrycode` IN('". implode(',', $needle)."')"; 
   $result = mysql_query($sql) or die(mysql_error());   
   if (!mysql_num_rows()) 
   {
      return false;
   }
 
   $return= array();
   while ($row = mysq_fetch_assoc($result))
   {
      $return[] = $row;
   }
   return $return;
}