Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mysql join statement with OR clause

Posted on 2014-12-25
3
Medium Priority
?
124 Views
Last Modified: 2015-01-15
I have a mysql statement as follows

SELECT o.status, COUNT(*) AS COUNT FROM orders o 
JOIN branch b ON o.brid=b.br_id AND b.`code`='NY'
WHERE o.created_ON >=DATE_SUB(CURDATE(),INTERVAL 90 DAY)
GROUP BY o.`status`;

Open in new window


This works fine, but now I need to add an or clause to this statement and I am not sure how to do this. The or clause is as follows;

OR o.shop_num IN (SELECT s.shop_no FROM shops s, branches b WHERE 
                    s.branch_id=b.branch_id AND b.branch_code='NY')

Open in new window


If I just embed this nested select statement into the query it takes too long to execute.

Thanks for the help.

A
0
Comment
Question by:aej1973
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 2000 total points
ID: 40518082
SELECT 
    o.status
    , COUNT(*) AS COUNT 
FROM orders o INNER JOIN branch b 
    ON o.brid=b.br_id                                                  
WHERE 
    (o.created_ON >=DATE_SUB(CURDATE(),INTERVAL 90 DAY) 
    AND b.`code`='NY') OR o.shop_num IN (SELECT s.shop_no FROM shops s INNER JOIN branches b  
    ON s.branch_id=b.branch_id Where b.branch_code='NY')
GROUP BY o.`status`;

Open in new window

0
 

Author Comment

by:aej1973
ID: 40551050
Thank you, this works great.
0
 

Author Closing Comment

by:aej1973
ID: 40551052
Thank you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

783 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