Forum Moderators: open

Message Too Old, No Replies

Get specific items from database

Select * from TABLE where item = '1' AND item = '2'

         

Alternative Future

7:10 pm on Jan 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I am trying to get results from my database where the item might be in the query many times. What is the most efficient query to achieve this?

SELECT *
FROM table
WHERE item = '1'
AND item = '2'
AND item = '25'
AND item = '105'
AND item = '990'

Is there a more efficient query that I should be using?

TIA,

-george

syber

7:56 pm on Jan 24, 2006 (gmt 0)

10+ Year Member



SELECT *
FROM table
WHERE item = '1'
AND item = '2'
AND item = '25'
AND item = '105'
AND item = '990'

You will get no rows back from this query because a particular column in a row can only have one value.

You probably meant to use OR.

SELECT *
FROM table
WHERE item = '1'
OR item = '2'
OR item = '25'
OR item = '105'
OR item = '990'

If so, you could rewrite the query as:

SELECT *
FROM table
WHERE item IN ('1', '2', '25', '105', '990')

It doesn't run any faster but is easier to follow and maintain.

Alternative Future

8:14 pm on Jan 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks syber

Perfect.