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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SELECT statement help 2

Status
Not open for further replies.

JPATX2

Technical User
Jun 8, 2004
3
I have the following query.

SELECT * FROM projects WHERE assembly_type = '2' OR assembly_type = '6' OR assembly_type = '10' OR assembly_type = '14' OR assembly_type = '18' OR assembly_type = '22' OR assembly_type = '26' OR assembly_type = '30' AND status != 'Complete' ORDER BY pnum ASC

It seems to ignore the "AND status !='Complete' " portion. I don't get any errors, but I do not get the results I want. I'm not a very experienced sql person so I'm not even sure if you can use AND and OR in the same statement or if there is alternative. Can someone help me with how I can accomplish this task.

Thank you in advance.
-JP
 
You can't mix AND and OR without associating them with ().

This should work:
(assembly_type = '2' OR assembly_type = '6' OR assembly_type = '10' OR assembly_type = '14' OR assembly_type = '18' OR assembly_type = '22' OR assembly_type = '26' OR assembly_type = '30') AND status != 'Complete'

It might be easier to use IN like this
WHERE assembly_type IN ('2','6','10','14','18','22','26','30') AND status != 'Complete'

 

you actually can mix ANDs and ORs successfully without parentheses, but it's way more verbose

the equivalent would be

WHERE assembly_type = 2 AND status <> 'Complete'
OR assembly_type = 6 AND status <> 'Complete'
OR assembly_type = 10 AND status <> 'Complete'
OR assembly_type = 14 AND status <> 'Complete'
OR assembly_type = 18 AND status <> 'Complete'
OR assembly_type = 22 AND status <> 'Complete'
OR assembly_type = 26 AND status <> 'Complete'
OR assembly_type = 30 AND status <> 'Complete'

nobody thinks of coding it that way, but you can ;-)

p.s. note that numeric columns should be compared to integers, not strings, and also that <> is standard sql (!= isn't)

r937.com | rudy.ca
 
Thank you for both of your responses. Both worked and solved the issue, your help is much appreciated.

Thanks again!
-JP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top