Sizetrac

SQL SELECT WHERE IN

Here is a basic sql statement where you are looking for shirts that are red, white or blue.

SELECT * FROM `shirts` WHERE `color`='red' OR `color`='white' OR `color`='blue'

Here is the same request, more efficiently constructed using IN

SELECT * FROM `shirts` WHERE `color` IN ('red','white','blue')

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) . "')" ;

  • Digg
  • TwitThis
  • del.icio.us
  • Netvouz
  • description
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank
  • Technorati
  • Facebook
  • Google
  • LinkedIn
  • Live
  • MySpace
  • Ping.fm
  • Yahoo! Buzz
  • E-mail this story to a friend!



Home | MySQL | SQL SELECT WHERE IN