Solved

Mysql join statement with OR clause

Posted on 2014-12-25
3
117 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 33

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

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 (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. 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