Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
naveen22
Starting Member
2 Posts |
Posted - 2014-05-07 : 09:35:53
|
this is my queryselect * from table8 where dept='Mechanical' and date between '2014-04-01' and '2014-04-26' and status='Completed' or status='Pending'it returns other dept valuesNaveen Rajan |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-05-07 : 09:42:05
|
quote: Originally posted by naveen22 this is my querySELECT * FROM CustomersWHERE Country='Germany'or Country='Mexico';i want to fetch the data where the country be either germany or mexico. but it returns all the valuesNaveen Rajan
The way you have written it, it will return only if the Country is Germany or Mexico. Do you have additional conditions in the where clause? |
 |
|
naveen22
Starting Member
2 Posts |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-05-07 : 10:20:23
|
Not sure what you are asking. The query that you have shown the link to returns one row because there is only one row where the country and city matches. SELECT * FROM CustomersWHERE Country='Germany'AND City='Berlin'; Run this query and it returns 16 rows where country is Germany or MexicoSELECT * FROM CustomersWHERE Country='Germany'OR Country = 'Mexico' |
 |
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2014-05-08 : 01:30:51
|
try to use ()?quote: Originally posted by naveen22 this is my queryselect * from table8 where dept='Mechanical' and date between '2014-04-01' and '2014-04-26' and (status='Completed' or status='Pending')it returns other dept valuesNaveen Rajan
|
 |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2014-05-08 : 08:36:13
|
To expand on what waterduck is suggesting, I think what you need is this:select * from table8 where dept='Mechanical' and date between '2014-04-01' and '2014-04-26' and (status='Completed' or status='Pending') This is because of operator precedence - http://technet.microsoft.com/en-us/library/ms190276.aspx i.e., AND has higher precedence than OR.This is the reason I was asking in my original reply whether you had other conditions the where clause. If you had posted the actual query to begin with, you could have resolved the issue much sooner. |
 |
|
|
|
|