Solved

Mysql select all companies when all groups are true

Posted on 2012-03-16
6
247 Views
Last Modified: 2012-03-16
Hello,

I have the following 2 tables (I simplified it here):

Customer:
`cust_id` int(10) unsigned NOT NULL auto_increment
 `cust_name` varchar(100) default NULL

Customer_groups  -> a customer can belong to many groups at the same time
`cust_id` int(10) unsigned NOT NULL
`group_id` int(10) unsigned NOT NULL

I need to select those customers who belong to ALL the selected groups.

For example let's say that the Customer_groups would have:

cust_id                   group_id
1                                   1
1                                   2
2                                   1
2                                   2
2                                   3
3                                    3
3                                    4
4                                    12
5                                      1
5                                      2
5                                      3
5                                      9
6                                       9

I want to select only those customers who belongs to group 1, group 2 and group 3.

So the result would be: customer 2 and customer 5.

How can I make such a query?

Thank you
0
Comment
Question by:starhu
[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
  • 4
6 Comments
 
LVL 10

Expert Comment

by:Mez4343
ID: 37730433
Based on your sample data the result would be Customers 1,2,3 and 5 since they belong to groups(1,2 or 3). I assume you only want the query result to return the Cust_id if the cust is in the list of groups, so added the distinct keyword. Try this


SELECT DISTINCT customer
FROM Customer a,  Customer_groups b
WHERE a.cust_id = b.cust_id
AND b.group_Id in (1,2,3)
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 37730436
try this:

SELECT * 
FROM   customer 
WHERE  cust_id IN (SELECT cust_id 
                   FROM   customer_groups 
                   WHERE  group_id IN ( 1, 2, 3 ) 
                   GROUP  BY cust_id 
                   HAVING COUNT(1) = 3) 

Open in new window


if it works we can write a better performing query with joins instead of using "in"
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 37730449
like this

SELECT c.* 
FROM   customer c 
       INNER JOIN (SELECT cust_id 
                   FROM   customer_groups 
                   WHERE  group_id IN ( 1, 2, 3 ) 
                   GROUP  BY cust_id 
                   HAVING COUNT(1) = 3) cg 
         ON cg.cust_id = cg.cust_id 

Open in new window

0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 37730461
oops, my solutions are to find customers who belongs to ALL not ANY :)

just remove HAVING COUNT(1) = 3 from above query

SELECT c.* 
FROM   customer c 
       INNER JOIN (SELECT cust_id 
                   FROM   customer_groups 
                   WHERE  group_id IN ( 1, 2, 3 ) 
                   GROUP  BY cust_id) cg 
         ON cg.cust_id = cg.cust_id 

Open in new window


or

SELECT c.* 
FROM   customer c 
       INNER JOIN (SELECT unique cust_id 
                   FROM   customer_groups 
                   WHERE  group_id IN ( 1, 2, 3 )) cg 
         ON cg.cust_id = cg.cust_id 

Open in new window

0
 

Author Comment

by:starhu
ID: 37730506
This works :

SELECT c.* 
FROM   customer c 
       INNER JOIN (SELECT cust_id 
                   FROM   customer_groups 
                   WHERE  group_id IN ( 1, 2, 3 ) 
                   GROUP  BY cust_id 
                   HAVING COUNT(1) = 3) cg 
         ON cg.cust_id = cg.cust_id 
                                            

Open in new window


... but can you somehow transform the select in such a form:
SELECT c.*
FROM   customer c
 where ....

The  customer table is an editable data grid in a Windows application, and the commercial Mysql plugin behaves erratically when there is a join in the select (the internal update mechanism will try to update the other table as well).

Thank you in advance
0
 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 37730528
try

SELECT c.*
FROM   customer c  where cust_id in
(SELECT cust_id
                   FROM   customer_groups
                   WHERE  group_id IN ( 1, 2, 3 )
                   GROUP  BY cust_id
                   HAVING COUNT(1) = 3)

for "in ALL GROUPS" or

SELECT c.*
FROM   customer c  where cust_id in
(SELECT unique cust_id
                   FROM   customer_groups
                   WHERE  group_id IN ( 1, 2, 3 )
)

for "in ANY GROUPS"
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Introduction In this article, I will by showing a nice little trick for MySQL similar to that of my previous EE Article for SQLite (http://www.sqlite.org/), A SQLite Tidbit: Quick Numbers Table Generation (http://www.experts-exchange.com/A_3570.htm…
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

763 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