[英]How to select only the unique records
By that I mean leaving any duplicate records 我的意思是留下任何重复的记录
For example 例如
ID NAME
1 a
2 a
3 b
4 b
5 c
Desired output. 所需的输出。
5 c only 仅5 c
I am tired of trying this . 我厌倦了尝试这个。 So I don't think I don't have any reasonably code to paste here .
所以我认为我没有任何可粘贴的代码。
Here is one way: 这是一种方法:
select t.*
from table t
where not exists (select 1
from table t2
where t2.name = t.name and t2.id <> t.id
);
Here is another way: 这是另一种方式:
select t.*
from table t join
(select name, count(*) as cnt
from table t
group by name
having cnt = 1
) tt
on tt.name = t.name;
这是另一种可能的方法:
select min(id), name from table group by name having count(*) = 1
I think the cleanest way to do this is to select the name and id for each row, group by the name, and filter only on values that have a COUNT(*) of 1. This means any rows that have a name that is not unique are excluded. 我认为最干净的方法是为每一行选择名称和ID,按名称分组,并仅对COUNT(*)为1的值进行过滤。这意味着名称不属于任何行唯一的除外。
It would look like this: 它看起来像这样:
SELECT id, name
FROM myTable
GROUP BY name
HAVING COUNT(*) = 1;
I can't get SQL to work, but verified this in MySQL workbench: 我无法使SQL正常工作,但在MySQL工作台中对此进行了验证:
Here is yet another way to do it: 这是另一种方法:
SELECT * FROM table AS A
WHERE (SELECT COUNT(*) FROM table AS T
WHERE T.NAME = A.NAME) = 1
select a.*
from table a
left join table b on a.name = b.name and a.id <> b.id
where b.id is null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.