Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Am I doing something wrong here, it's a simple query and it's not work 1

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
I have a simple query that consistantly returns the wrong results which makes me think I'm missing something here.
The query is:

SELECT * FROM `helpdesk` WHERE user = 'smith' OR mgr = 'smith' AND complete = '2'

This should return all rows with smith as the user or the manager and only if the 'complete' value is 2.

However I get complete with 1 as the value in my result set.

Can anyone tell me what I'm doing wrong?
 
what you are doing wrong is mixing ANDs and ORs without the appropriate parentheses for your requirements

ANDs take precedence over ORs

what you wrote --

WHERE user = 'smith' OR mgr = 'smith' AND complete = '2'

this is equivalent to --

WHERE user = 'smith' OR ( mgr = 'smith' AND complete = '2' )

what you probably wanted --

WHERE ( user = 'smith' OR mgr = 'smith' ) AND complete = '2'

see the difference? ;-)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
I do see the difference. I tried it and it works. I can't believe I've never ran into this before over the past 4 years that I've been doing this.

I really appreciate your fast response, have a great day.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top