我需要在我的MySQL数据库中的表中找到具有相同名称但具有不同sport的重复值。下面是一个数据示例:
John Smith Athletics
Edward Green Athletics
Edward Green Fencing
Jim Brown Rugby
Jim Brown Rowing
Jim Brown Sailing
Stan Smith Football
Stan Smith Football好的,我想进行一个查询,它给出了这个结果:
Edward Green Athletics
Edward Green Fencing
Jim Brown Rugby
Jim Brown Rowing
Jim Brown Sailing就像我说的,只有同名不同运动的价值观,才能找到同名的。
发布于 2016-08-19 01:36:07
以下是使用exists的一种选择
select *
from yourtable t
where exists (
select 1
from yourtable t2
where t.name = t2.name and t.sport != t2.sport
)发布于 2021-12-19 20:41:08
SELECT name
FROM (SELECT DISTINCT name, sport FROM My_table) AS Temporary_table_name
GROUP BY name
HAVING ( COUNT(name) > 1 )使用命令
SELECT DISTINCT name, sport你排除了名字和运动的重复组合(即"Stan Smith - Football"),然后你可以放心地寻找重复的名字(因为你知道它们不会指同一项运动)
发布于 2016-08-19 01:40:48
内部select获取具有多个不同sport的所有name。要获得这些名称的运动,您必须在同一张表上加入
select t1.*
from your_table t1
join
(
select name
from your_table
group by name
having count(distinct sport) > 1
) t2 on t1.name = t2.namehttps://stackoverflow.com/questions/39024294
复制相似问题