Solved

Mysql join statement with OR clause

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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