Philll_t
Philll_t

Reputation: 4437

Best way to sql query a table based on a list of emails

I have a list of emails who's records I need to pull. I'm sure I'm doing this the wrong way:

SELECT * FROM `MY_TABLE` 
WHERE 
field_1 = 'something'
AND
created BETWEEN '2014-06-01 00:00:00' AND '2014-06-30 23:59:59'
AND 
   email ="[email protected]"
OR email ="[email protected]"
OR email ="[email protected]"

this returned all the emails with the accounts but ignored field_1, so they were all different. Is there a better way? The list of emails is over 100 long. Thanks.

Upvotes: 1

Views: 2561

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269873

You should use in:

SELECT *
FROM `MY_TABLE` 
WHERE field_1 = 'something' AND
      created BETWEEN '2014-06-01 00:00:00' AND '2014-06-30 23:59:59' AND 
      email IN ('[email protected]', '[email protected]', '[email protected]');

You should use single quotes for string and date constants.

Upvotes: 4

Siva Kumar
Siva Kumar

Reputation: 2006

Use bracket and try

 SELECT * FROM `MY_TABLE` 
    WHERE 
    field_1 = 'something'
    AND
    created BETWEEN '2014-06-01 00:00:00' AND '2014-06-30 23:59:59'
    AND 
       (email ="[email protected]"
    OR email ="[email protected]"
    OR email ="[email protected]")

Upvotes: 1

Related Questions