3

I'm new to Mysql & have a table where at presently i'm fetching data as

SELECT * FROM tablename WHERE name='$user' AND date='$selecteddate'

Now i want to add 1 more column named status_id where i want to select from to values i.e 1 or 2

I tried this query

 SELECT * FROM tablename WHERE (name='Ankit') AND (date='2015-04-23') AND (status_id='1' OR status_id='2')

but didn't work out.

Please help.

Saty
  • 22,443
  • 7
  • 33
  • 51
AK_56
  • 161
  • 1
  • 2
  • 17

3 Answers3

8

Well, you just need the elements concerned by the or clause to be between parenthesis.

They are not "needed", by the way (you could use precedence order of AND / OR), but I would say this is the best way for readability.

And if status_id data type is int, you don't need the ' ' around 1, and 2.

SELECT * 
 FROM tablename 
WHERE name='Ankit' 
AND date='2015-04-23' 
AND (status_id=1 OR status_id=2) 

By the way, in this case, you may use an IN clause

AND status_id in (1, 2)
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
1

Something like this

"SELECT * FROM tablename 
 WHERE name='Ankit' 
 AND date='2015-04-23' 
 AND (status_id='1' OR status_id='2')";
Tom
  • 691
  • 4
  • 7
0

Try with using IN

AND status_id IN (1, 2)
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Rex Rex
  • 1,030
  • 1
  • 8
  • 29