简体   繁体   中英

Best way to limit results in MySQL with user subcategories

I am trying to essentially solve for the following:

1) Find all users in the system who ONLY have programID 1.

2) Find all users in the system who have programID 1 AND any other active program.

My tables structures (in very simple terms are as follows):

users

userID  | Name
================
1       | John Smith
2       | Lewis Black
3       | Mickey Mantle
4       | Babe Ruth
5       | Tommy Bahama

plans

ID | userID | plan | status
---------------------------
1  |  1     | 1    | 1
2  |  1     | 2    | 1
3  |  1     | 3    | 1
4  |  2     | 1    | 1
5  |  2     | 3    | 1
6  |  3     | 1    | 0
7  |  3     | 2    | 1
8  |  3     | 3    | 1
9  |  3     | 4    | 1
10 |  4     | 2    | 1
11 |  4     | 4    | 1
12 |  5     | 1    | 1

I know I can easily find all members with a specific plan with something like the following:

SELECT * FROM users a JOIN plans b ON (a.userID = b.userID) WHERE b.plan = 1 AND b.status = 1

but this will only tell me which users have an 'active' plan 1.

How can I tell who ONLY has plan 1 (in this case only userID 5) and how to tell who has plan 1 AND any other active plan?

Update: This is not to get a count, I will actually need the original member information, including all the plans they have so a COUNT(*) response may not be what I'm trying to achieve.

Okey..., You have mentioned that you don't want a count of the other plans that particular user hold, but according to your requirement you can have optimum output like,

this many user have plan 1 active and this is the count of the other active plan he have exept plan one.

SELECT u.userID,
   u.Name,
   p.Id AS Plan1ActiveId,
   (SELECT COUNT(*) FROM plans sub where sub.userID = u.userID and sub.plan <> 1 and sub.status = 1) AS OtherActivePlanCount
FROM users u JOIN plans p ON (u.userID = p.userID) 
where p.plan = 1 AND p.status = 1;

or if you want to drill it down towards the details of the plan you need to apply second query that bring you other plans' detail.

Well you can make use of the COUNT combined with a GROUP BY and the HAVING clause.

For example:

SELECT * FROM users a JOIN plans b ON (a.userID = b.userID) GROUP BY b.user_ID HAVING COUNT(*) = 1 AND b.plan = 1

This will group rows by user id, and only include rows where the count = 1 (only 1 line in the plans table)

Note - I ended up simplifying the overall concept in my head. I simply did a subselect of all members who I knew had the plan I was looking to target. I then did an overall select of all plans by members who either had IDs that were IN or NOT IN the subselect of known ids).

After testing, this was much less resource intensive and a bit easier to use than the suggestions but appreciate the help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM