Thursday, October 01, 2009

MySQL IN clause gem

Well today was a good day as I found a GEM for the MySQL IN clause. Normally I would use the IN clause to search for multiple values in a field like this:


SELECT *
FROM tbl_name
WHERE field_name
IN ('value_1', 'value_2', 'value_3', 'value_x')


But today discovered that you can reverse the fields and values. So instead of using the OR clause, you could use something like this:


SELECT *
FROM tbl_name
WHERE value_x
IN ('field_name_1', 'field_name_2', 'field_name_3', 'field_name_x')


This makes it super simple to search for one value across multiple fields.