Simplifying a WHERE clause

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Simplifying a WHERE clause

Post by Reviresco »

(MYSQL) - Is there a way to simplify or use a shortcut on multiple OR operators in a WHERE clause?

Instead of:

Code: Select all

WHERE field1 = '$x' OR field2 = '$x' OR field3 = '$x' OR field4 = '$x'
A way to combine those four fields into one big "OR"?

(Also, same question for REGEXP instead of "="?)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Simplifying a WHERE clause

Post by Chris Corbyn »

See if this works:

[sql]WHERE '$x' IN (field1, field2, field3, field4)[/sql]

IN() works for values, I've never tried it with fields like that but logically it should work.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Simplifying a WHERE clause

Post by Reviresco »

Yep that works (for the "=" anyway, not regexp).

For regexp, I think I'll try fulltext indexing instead.
Post Reply