[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

problem with mysql statement OR

Posted on 2009-07-03
5
Medium Priority
?
200 Views
Last Modified: 2013-12-13
Hi there,

for some reason when i use this mysql statement:

SELECT COUNT(*) FROM projects JOIN link_projects_zones ON projects.proj_ID = link_projects_zones.proj_ID WHERE link_projects_zones.zone_ID = 1 AND projects.proj_Status = 2 or 3 or 4

It returns the entire number of records in the projects table rather than just the records where proj_Status = 2,3 or 4. I am having the same problem when querying "2 or 3" and basically anything with OR in it. Any ideas?

0
Comment
Question by:cookiejest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 1000 total points
ID: 24773603
AND (projects.proj_Status = 2 or  projects.proj_Status = 3 or  projects.proj_Status  = 4 )
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 1000 total points
ID: 24773607
AND projects.proj_Status IN ( 2, 3 , 4 )

OR

AND projects.proj_Status BETWEEN 2 AND 4
0
 
LVL 5

Accepted Solution

by:
Opalelement earned 1000 total points
ID: 24773638
Just to help him out for next time, I am going to tell him what was wrong.

The query:

SELECT COUNT(*) FROM projects JOIN link_projects_zones ON projects.proj_ID = link_projects_zones.proj_ID WHERE link_projects_zones.zone_ID = 1 AND projects.proj_Status = 2 or 3 or 4

That "or 3 or 4" is evaluating each expression individually. A single positive number will always be true, so since 3 and 4 are considered to be individual expressions, they are both ture. Since you said "or", it only needs one true to select it all.

To fix yours, simply add the "projects.proj_Status = " before the 3 and the 4, like so:
SELECT COUNT(*) FROM projects JOIN link_projects_zones ON projects.proj_ID = link_projects_zones.proj_ID WHERE link_projects_zones.zone_ID = 1 AND projects.proj_Status = 2 or projects.proj_Status = 3 or projects.proj_Status = 4
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24773712
When you combine AND and OR in the same expression, it is important to use parantheses to make it clear what is meant. Note the difference between these expressions:

link_projects_zones.zone_ID = 1 AND (projects.proj_Status = 2 or projects.proj_Status = 3 or projects.proj_Status = 4)
(link_projects_zones.zone_ID = 1 AND projects.proj_Status = 2) or projects.proj_Status = 3 or projects.proj_Status = 4

The last expression will ignore the zone_ID=1 requirement if proj_Status is 3 or 4. Without the parantheses, this is how MySQL interprets it.
0
 

Author Closing Comment

by:cookiejest
ID: 31599629
thanks for all the answers!
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question