tags:

views:

43

answers:

3

Situation:

Have a SET field called country with values

('us','uk','fr','intl')

When I go to pull the content from the db:

$sql  = "SELECT id, title, content "; 
$sql .= "FROM table_name "; 
$sql .= "WHERE country='us'";

Works fine with entries labeled only 'us' but if the entry is 'us,uk' or 'us,fr,intl' and so forth. It does not pull those. Ideas?

Thanks!

+4  A: 

You'd need to use the FIND_IN_SET() function.

SELECT id, title, content
FROM table_name
WHERE FIND_IN_SET('us', country)
VoteyDisciple
Thank you so much! I admit I'm a newbie but how the heck would you know something like that?
HelpAppreciated
Since I've never used the `SET` datatype, I myself didn't know the function syntax offhand. I Googled "MySQL SET" which finds the documentation: http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html (While you're there, I recommend the "Why You Shouldn't Use SET" section.)
VoteyDisciple
+1  A: 
$sql  = "SELECT id, title, content "; 
$sql .= "FROM table_name "; 
$sql .= "WHERE country in ('us','uk','fr','intl')";
adatapost
This doesn’t work if the set `country` has more than one value.
Gumbo
A: 
$sql  = "SELECT id, title, content FROM table_name WHERE country in (";
$countries=split(",",$whatever);
$first=1;
foreach($countries as $country){
    if(!$first) $sql .= ",";
    $sql .= "'".mysql_real_escape_string($country)."'";
    $first=0;
}
sql .= ")";
knizz