Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                                                                                            
                
        
我有一张这样的桌子:

ID NAME  CASE_ID
1  John  33
2  Harry 33
3  James 55
4  John  55
5  John  44
6  Harry 66


我想找到至少有两种情况的人:

ID NAME  CASE_ID
1  John  33
4  John  55
5  John  44
6  Harry 66
2  Harry 33


我已经尝试过了,但是没有用:

SELECT name, case_id
FROM t
GROUP BY case_id
HAVING COUNT(name) > 1;

最佳答案

您在select中缺少逗号,从而改变了查询的含义:

select name, case_id
from t
group by case_id
having count(name) > 1;


您原来的查询将name列重命名为case_id,这不是您的意图。

如果需要原始数据,则将表join放回:

select t.*
from t join
     (select name, count(distinct case_id)
      from t
      group by name
      having count(distinct case_id) > 1
     ) nc
     on t.name = nc.name;

关于mysql - 查找列值至少出现两次的行? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27334448/

10-11 14:21