简体   繁体   English

删除所有重复的查询结果(不区分)

[英]Remove all query results that have duplicates (NOT DISTINCT)

I have a table with two sets of integer values. 我有一个带有两组整数值的表。 Using MySQL I want to display all of the rows that correspond to unique entries in the second column. 使用MySQL我想在第二列中显示与唯一条目相对应的所有行。 Basically I can have duplicate A values, but only unique B values. 基本上,我可以有重复的A值,但只能有唯一的B值。 If there are duplicates for a value in B, remove all the results with that value. 如果B中的某个值重复,则删除所有具有该值的结果。 If I use DISTINCT I will still get one of those duplicates which I do not want. 如果我使用DISTINCT我仍会得到我不想要的重复项之一。 I also want to avoid using COUNT() . 我也想避免使用COUNT() Here's an example: 这是一个例子:

|_A____B_|
| 1    2 |
| 1    3 |
| 2    2 |
| 2    4 |
| 1    4 |
| 5    5 |

Will have the following Results (1,3), (5,5). 将具有以下结果(1,3),(5,5)。 Any value in B that has a duplicate is removed. B中具有重复项的所有值都将被删除。

Try this 尝试这个

SELECT * FROM TEMP WHERE B IN (
  SELECT B FROM TEMP GROUP BY B Having COUNT(B)=1
 );

I know you want to avoid using COUNT() but this is the quick solution. 我知道您想避免使用COUNT()但这是快速的解决方案。

working fiddle here - http://sqlfiddle.com/#!9/29d16/8 在这里工作提琴-http://sqlfiddle.com/#!9/ 29d16/8

Tested and works! 经过测试,可以正常工作! you need atleast count(*) to count the values 您需要至少count(*)来计算值

 select * from test where B in (
   select B from test  group by B  having count(*)<2
 )

I don't know why you want to avoid using count(), because that's what would do the trick as follows: 我不知道您为什么要避免使用count(),因为这将达到以下目的:

Let's say your table is named "mytable" 假设您的表名为“ mytable”

SELECT t1.A, t1.B
FROM mytable t1
JOIN (
    SELECT B, count(*) AS B_INSTANCES
    FROM mytable
    GROUP BY B
    HAVING count(*) = 1
) t2 ON t2.B = t1.B
ORDER BY t1.A, t1.B

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

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