简体   繁体   中英

Mysql Query Understanding

I have a quick question about how to query this information

So let's say in my database I have topics of

Welcome Center    Group 1
Members Questions Group 2
Admin Ops         Group 3

and a users table of

User 1      Group 1
User 2      Group 2
User 3      Group 3

and a group tables of

1     Register User  
2     Member 
3     Admin   

Okay so that would be something like a database, now say I only want register a user with a group 1 to see Items with group 1.

"SELECT * FROM topic WHERE group = 1"

So they can't see the stuff for the member's and admin's.

This is where the problem starts.

I need to find a query where, or learn away that we allow the member to see the registered users topic and the members topic.

"SELECT * FROM topic WHERE group = 1 AND group = 2"

As you can see this an example and not sure if this is what I need to do.

Same with Admins, I need them to be able to see the registered user topics, member topics, and the admin topics. So all three.

"SELECT * FROM topic WHERE group = 1 AND group = 2 AND group = 3"

As you can see that I am not one hundred percent sure that this will work, before I waste my time coding something that isn't going to be like this but the query is where I am kinda stuck at.

If you want all topics that have both groups, then use aggregation and a having clause:

SELECT t.topic
FROM topic t
GROUP BY t.topic
HAVING SUM(group = 1) > 0 AND
       SUM(group = 2) > 0;

Note that group is a bad name for a column because it is a reserved word.

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