Mysql join statement with OR clause

Posted on 2014-12-25
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'
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.

Question by:aej1973
  • 2
LVL 33

Accepted Solution

Mike Eghtebas earned 500 total points
ID: 40518082
    , COUNT(*) AS COUNT 
FROM orders o INNER JOIN branch b 
    ON o.brid=b.br_id                                                  
    (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


Author Comment

ID: 40551050
Thank you, this works great.

Author Closing Comment

ID: 40551052
Thank you.

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

Suggested Solutions

Title # Comments Views Activity
MySQL going to sleep 4 62
sql statement - php 5 46
MySQL backend - what should be front end? 8 40
showing numeric numbers 2 0
All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

15 Experts available now in Live!

Get 1:1 Help Now