简体   繁体   English

两列组合唯一的SQL查询(带有子查询)

[英]SQL query where combination of two columns is unique (with sub-query)

I have two tables; 我有两个桌子。 one with contact info, and one with a list of groups. 一个带有联系信息,另一个带有组列表。 Eg: 例如:

clients
+-------------+---------------+-------------+------------+
|  client_id  |  client_name  |  group1_id  |  group2_id |
+-------------+---------------+-------------+------------+
| 283         | John Smith    | 1           | null       |
| 284         | Jane Smith    | 1           | 2          |
| 285         | Tim Johnson   | 1           | null       |
| 286         | Peter Guy     | null        | null       |
+-------------+---------------+-------------+------------+

groups
+------------+----------------+
|  group_id  |  group_name    |
+------------+----------------+
|  1         |  Smith Group   |
|  2         |  Johnson Group |
+------------+----------------+

In case it's not self-evident, group1_id and group2_id are foreign keys into the groups table. 如果不言而喻,group1_id和group2_id是groups表中的外键。

I need to write a query to select the client name and group name. 我需要编写查询以选择客户端名称和组名称。 If a client is a member of two groups (like Jane Smith) they should appear twice in the output. 如果客户端是两个组的成员(例如Jane Smith),则它们应在输出中出现两次。 Ideally each line should output the group name, followed by the client name in parenthesis, and all results should be sorted alphabetically. 理想情况下,每行应输出组名,然后在括号中输出客户名,并且所有结果应按字母顺序排序。 So for the above tables the output should be: 因此,对于以上表,输出应为:

John Smith (Jane Smith)
Johnson Group (Jane Smith)
Johnson Group (Tim Johnson)
Smith Group (John Smith)

Is this possible using a single SQL query? 是否可以使用单个SQL查询?

I'm using MySQL. 我正在使用MySQL。 I'm in the planning stage of the project at the moment so if I need to change the database schema to better support doing I can. 目前,我正处于项目的计划阶段,因此,如果需要更改数据库架构以更好地支持,则可以。

Thanks in advance. 提前致谢。

Yes this is possible using a join with in or or . 是的,可以通过使用inor进行join

select c.client_name, g.group_name
from clients c 
   join groups g on g.group_id in (c.group1_id, c.group2_id)

However, I would suggest looking into database normalization . 但是,我建议您研究database normalization Altering your table structure to include a 3rd table called ClientGroup would be better to work as a junction table. 更改表结构以包含名为ClientGroup的第三个表最好用作联结表。 Then you would end up with something like this: 然后,您将得到如下结果:

clients (client_id, client_name)
groups (group_id, group_name)
clientgroup (client_id, group_id)

And then you could just join the tables again together: 然后,你可以只join表再次合作:

select c.client_name, g.group_name
from clients c
    join clientgroup cg on c.client_id = cg.client_id
    join groups g on cg.group_id = c.group_id

If you want two rows when a client matches twice, then use or : 如果客户端匹配两次时希望两行,请使用or

select c.client_name, g.group_name
from clients c join
     groups g
     on g.group_id = c.group1_id or g.group_id = c.group2_id;

This formats the results as two columns, rather than a string that puts the group name in parentheses. 这样会将结果格式化为两列,而不是将组名放在括号中的字符串。

Thank you sgeddes and Gordon for your quick and useful answers. 谢谢sgeddes和Gordon的快速有用的回答。 I figured I'd need a join but was having trouble getting the syntax right (I've not used joins before). 我认为我需要一个连接,但是在正确使用语法时遇到了麻烦(我以前没有使用过连接)。 The end result was quite obvious after seeing someone else give the answer! 在看到其他人给出答案之后,最终结果非常明显!

With the sorting and client name in parenthesis, the final query is: 括号中带有排序和客户名称,最终查询为:

SELECT client_id, CONCAT(g.group_name, " (", c.client_name, ")") new_col
FROM clients c 
    JOIN groups g on g.group_id IN (c.group1_id, c.group2_id)
ORDER BY g.group_id, c.client_name ASC

I'm a first-time poster and can't see a way to award an answer. 我是初次发布者,看不到授予答案的方法。 Am I missing something, or is it only questions that offer a bounty that allow awarding? 我是否缺少某些东西,还是仅是提供赏金的问题才可以获奖?

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

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