SQL SELECT WHERE IN
Like this blog? Consider exploring one of our sponsored banner ads...
Here is a basic sql statement where you are looking for shirts that are red, white or blue.
[mysql]SELECT * FROM `shirts` WHERE `color`=’red’ OR `color`=’white’ OR `color`=’blue’[/mysql]
Here is the same request, more efficiently constructed using IN
[mysql]SELECT * FROM `shirts` WHERE `color` IN (‘red’,'white’,'blue’)[/mysql]
If you are like me and dynamically create SQL statements using PHP then you get the idea of how easy it would be to create this statement. Let’s say you have an array of possible value.
$colors = array('red','white','blue'); $sql = "SELECT * FROM `shirts` WHERE `color` IN ('" . implode("','", $colors) . "')" ;
3 Comments
Jump to comment form | comments rss [?]