SELECT * FROM `TABLE` WHERE
(`PRIMARY_KEY`= `VALUE1`) OR
(`PRIMARY_KEY`= `VALUE2`) OR
(`PRIMARY_KEY`= `VALUE3`) OR
(`PRIMARY_KEY`= `VALUE4`) OR
(`PRIMARY_KEY`= `VALUE5`) OR ...
This works. But is there a faster way?
SELECT * FROM `TABLE` WHERE
(`PRIMARY_KEY`= `VALUE1`) OR
(`PRIMARY_KEY`= `VALUE2`) OR
(`PRIMARY_KEY`= `VALUE3`) OR
(`PRIMARY_KEY`= `VALUE4`) OR
(`PRIMARY_KEY`= `VALUE5`) OR ...
This works. But is there a faster way?
SELECT * FROM table WHERE primary_key IN (value1, value2, ...)
You can use the IN
keyword to achieve this:
SELECT * FROM Table WHERE PRIMARY_KEY IN (VALUE1, VALUE2, ...)
Using the value in (list)
construct is not faster, but the SQL-code will be much easier to read/understand once someone needs to maintain the code.
SELECT *
FROM `TABLE`
WHERE `PRIMARY_KEY` in( `VALUE1`
, `VALUE2`
, `VALUE3`
, `VALUE4`
, `VALUE5`
)
Updated: Rewritten to reflect the feedback from the comments.
what about
SELECT * FROM `TABLE` WHERE
(`PRIMARY_KEY`IN( `VALUE1`,`VALUE2`,`VALUE3`,`VALUE4`,`VALUE5`) )