简体   繁体   English

SQL:筛选出列值多次出现的行

[英]SQL: Filter out rows where column value occurs more than once

I have a MySQL table that looks like this: 我有一个如下所示的MySQL表:

id    |    label
----------------
1          "john"
1          "henry"
1          "sara"
2          "henry"
3          "tim"

So a given id can have multiple label s. 因此,给定的id可以具有多个label I want to keep only the rows where the id has a single label . 我只想保留id具有单个label So the correct output for the above table would be: 因此,上表的正确输出为:

id    |    label
----------------
2          "henry"
3          "tim"

I was thinking I should group by id and find the count of labels for each id . 我当时想我应该按id分组并找到每个id的标签数。 Then I'd take only rows with a count of 1. 然后,我只需要计数为1的行。

WITH temp as
(SELECT id
FROM original_table
GROUP BY id
HAVING COUNT(id) > 5)

SELECT *
FROM original_table ot
WHERE ot.id in temp.id

Does that look close? 看起来很近吗?

Thanks! 谢谢!

You could just use a join to only include ID's that occur once in a sub-query: 您可以只使用联接仅包含在子查询中出现一次的ID:

SELECT  id,
        label
  FROM  original_table ot
    INNER JOIN  (
                SELECT  id
                  FROM  original_table
                  GROUP BY id
                  HAVING COUNT(*) = 1
                ) a ON a.id = ot.id;

Or you could use an IN clause: 或者您可以使用IN子句:

SELECT  id,
        label
  FROM  original_table
  WHERE id IN (SELECT   id
                  FROM  original_table
                  GROUP BY id
                  HAVING COUNT(*) = 1
              );

I think aggregation is the simplest method: 我认为聚合是最简单的方法:

select id, min(label) as label
from original_table t
group by id
having count(*) = 1;

You could try this: 您可以尝试以下方法:

SELECT t.id, t.label
FROM tbl AS t
JOIN (SELECT id FROM tbl GROUP BY id HAVING count(label) = 1) AS t1
ON t.id = t1.id;

Assuming that the pair id and label is unique, you can use NOT EXISTS and a correlated subquery. 假设对idlabel是唯一的,则可以使用NOT EXISTS和相关的子查询。

SELECT t1.id,
       t1.label
       FROM original_table t1
       WHERE NOT EXISTS (SELECT *
                                FROM original_table t2
                                WHERE t2.id = t1.id
                                      AND t2.label <> t1.label);

Yes your approach is correct and you may have to change your having count condition and while referring to CTE you may have to change your syntax little , but you can do it without CTE as well in the same line with exists condition. 是的,您的方法是正确的,您可能必须更改您的计数条件,而在引用CTE时,您可能需要稍微更改语法,但是在存在条件的同一行中也可以不使用CTE来执行此操作。

Create table  temp  (ID int , Label varchar(10)); 

insert into  temp values 
(1  ,      "john" ), 
(1   ,     "henry" ) , 
(1   ,     "sara" )  , 
( 2   ,    "henry"  ) , 
(3   ,     "tim" ) ; 


select t.ID , t.Label from temp t 
where exists (
select ID, count(1) Dups  from temp t1 where t1.ID = t.ID group by ID having count(1) 
= 1) 

Output: 输出:

    ID, Label
    2, henry
    3, tim

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

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