简体   繁体   English

MySQL查询理解

[英]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. 好的,那就像是一个数据库,现在说我只想注册一个组1的用户才能看到组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: 如果要使所有主题都具有两个组,请使用aggregation和having子句:

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. 请注意, group是列的坏名称,因为它是保留字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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