[英]MySQL select only unique values
I have two tables.我有两张桌子。
Clients:客户:
+--------------+-------------+
| CLIENT_ID | LABEL |
+--------------+-------------+
| 123 | label1 |
+--------------+-------------+
| 123 | label3 |
+--------------+-------------+
| 456 | label1 |
+--------------+-------------+
| 789 | label2 |
+--------------+-------------+
| 987 | label2 |
+--------------+-------------+
| 987 | label4 |
+--------------+-------------+
Managers:经理:
+----+--------------+
| ID | CLIENT_ID |
+----+--------------+
| 1 | 123 |
+----+--------------+
| 1 | 456 |
+----+--------------+
| 2 | 456 |
+----+--------------+
| 3 | 789 |
+----+--------------+
| 3 | 987 |
+----+--------------+
| 4 | 789 |
+----+--------------+
I need to select ID from Managers which have only clients with labels "label1" or "label2" and do not have clients with other labels.我需要来自经理的 select ID,这些经理只有带有标签“label1”或“label2”的客户,而没有带有其他标签的客户。 The resulting output should be like 2 and 4.
生成的 output 应该类似于 2 和 4。
I tried to do as我试着做
select m.id
from managers m
join clients c on m.client_id = c.client_id
where c.label in ('label1', 'label2');
but it returns all ids.但它返回所有ID。
Close.关。 Just add aggregation:
只需添加聚合:
select m.id
from managers s join
clients c
on m.client_id = c.client_id
where c.label in ('label1', 'label2')
group by m.id
having count(distinct c.label) = 2;
If you want only clients with these two labels, then use:如果您只想要具有这两个标签的客户端,请使用:
select m.id
from managers s join
clients c
on m.client_id = c.client_id
group by m.id
having count(distinct c.label) = 2;
select m.id
from managers s join
clients c
on m.client_id = c.client_id
group by m.id
having count(distinct case when c.label in ('label1', 'label2') then c.label end) = 2 and
count(distinct c.label) = 2;
Or, more efficiently:或者,更有效地:
having sum(c.label = 'label1') > 0 and
sum(c.label = 'label2') > 0 and
sum(c.label not in ('label1', 'label2')) = 0;
use group by
as follows:使用
group by
如下:
select m.id
from managers m
join clients c on m.client_id = c.client_id
where c.label in ('label1', 'label2')
group by m.id
having count(*) = 1;
Please try group by
and having
statement here to get unique records请尝试
group by
并having
此处声明以获取唯一记录
SELECT ID FROM `Clients` c
left join Managers m on (c.client_id = m.CLIENT_ID)
where LABEL in ('label1', 'label2')
group by ID having count(*) = 1
Thanks谢谢
You can use a CTE to get the number of occurrences where client labels associated with a particular manager are valid:您可以使用 CTE 获取与特定经理关联的客户标签有效的出现次数:
with r as (select m.id id, sum(c.label in ('label1', 'label2')) c1, count(*) c2
from managers m join clients c on m.client_id = c.client_id group by m.id)
select id from r where c1 = c2;
Output: Output:
id ![]() |
---|
2 ![]() |
4 ![]() |
Join the tables, group by manager and use conditional aggregation for your conditions in the HAVING
clause:加入表,按经理分组并在
HAVING
子句中为您的条件使用条件聚合:
SELECT m.id
FROM managers m INNER JOIN clients c
ON m.client_id = c.client_id
GROUP BY m.id
HAVING MAX(c.label IN ('label1', 'label2')) = 1
AND MAX(c.label NOT IN ('label1', 'label2')) = 0
This will also work if a client has both labels 'label1'
and 'label2'
and may be extended to more labels.如果客户端同时具有标签
'label1'
和'label2'
并且可以扩展到更多标签,这也将起作用。
See the demo .请参阅演示。
Results:结果:
id ![]() |
---|
2 ![]() |
4 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.