我有一个包含成员的表,我需要查找所有具有相同名称的成员。如何用SQL编写它?
这是表格:
create table Members(
MemberID char(13),
Name varchar(20),
primary key(MemberID)
)engine=innodb;
insert into Members(MemberID, Namn) values(’111’, ’John’);
insert into Members(MemberID, Namn) values(’222’, ’John’);
insert into Members(MemberID, Namn) values(’333’, ’Ralph’);
insert into Members(MemberID, Namn) values(’444’, ’Tony’);
insert into Members(MemberID, Namn) values(’555’, ’Ben’);
我需要的是为所有具有相同名称的成员提取MemberID - 在此示例中应返回111和222。
答案 0 :(得分:0)
这会对你有所帮助。
SELECT
name, COUNT(*)
FROM
<Table Name>
GROUP BY
name
HAVING
COUNT(*) > 1
答案 1 :(得分:0)
您可以使用此查询来获得所需的结果:
select * from Members where Name in(
select Name from Members
group by Name having count(Name) > 1
)
答案 2 :(得分:0)
WITH members_1 AS
(
SELECT UPPER (name) AS name1
FROM members
)
SELECT x.name1,y.memberid FROM
(
SELECT name1
FROM members_1
GROUP BY name1
HAVING COUNT(name1) > 1
) x
JOIN members y ON UPPER(x.name1) = UPPER(y.name)
答案 3 :(得分:0)
select memberid
from
(
select count(*) count,group_concat(memberid) memberid,name
from members group by name
) membertable
where membertable.count>1