Solved

problem with mysql statement OR

Posted on 2009-07-03
5
183 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
5 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 250 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 250 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 250 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now