Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择ID出现一次的所有记录_Mysql_Sql_Select_Distinct_Row Number - Fatal编程技术网

Mysql 选择ID出现一次的所有记录

Mysql 选择ID出现一次的所有记录,mysql,sql,select,distinct,row-number,Mysql,Sql,Select,Distinct,Row Number,我有一个表格: User1 A User2 A User2 B User3 C User4 A User4 C User5 D 因此,我只需要选择出现一次的用户。例如,从上面我想要User1,User3,User5。我可以使用类似row_number的东西来删除重复项,但是这将为所有用户返回一行,即使是那些有多个条目的用户,这不是我想要的。我也不能使用DISTINCT,因为例如user2a和user2b不相等,所以不会被捕获 使用分组方式: 如果您知道第二列中没有重复项,那么使用正确的索引,以

我有一个表格:

User1 A
User2 A
User2 B
User3 C
User4 A
User4 C
User5 D
因此,我只需要选择出现一次的用户。例如,从上面我想要User1,User3,User5。我可以使用类似row_number的东西来删除重复项,但是这将为所有用户返回一行,即使是那些有多个条目的用户,这不是我想要的。我也不能使用DISTINCT,因为例如user2a和user2b不相等,所以不会被捕获

使用分组方式:

如果您知道第二列中没有重复项,那么使用正确的索引,以下操作可能会更快:

select t.*
from t
where not exists (select 1 from t t2 where t2.username = t.username and t2.col2 <> t.col2);
我不知道你为什么提到排号。MySQL不支持该功能。
select t.*
from t
where not exists (select 1 from t t2 where t2.username = t.username and t2.col2 <> t.col2);