Solved

Mysql join statement with OR clause

Posted on 2014-12-25
3
122 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
[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
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 500 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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