0

我需要获取拥有多个成员的所有俱乐部的 Club_ID;

这是sql:

CREATE  TABLE `Members` (
  `ID` INT NOT NULL AUTO_INCREMENT ,
  `Club_ID` INT NULL ,
  `Name` VARCHAR(45) NULL ,
  PRIMARY KEY (`ID`) );


INSERT INTO `Members` (`Club_ID`, `Name`) VALUES ('1', 'Jim');
INSERT INTO `Members` (`Club_ID`, `Name`) VALUES ('2', 'John');
INSERT INTO `Members` (`Club_ID`, `Name`) VALUES ('1', 'Bill');
INSERT INTO `Members` (`Club_ID`, `Name`) VALUES ('3', 'Stan');
INSERT INTO `Members` (`Club_ID`, `Name`) VALUES ('4', 'David');
INSERT INTO `Members` (`Club_ID`, `Name`) VALUES ('1', 'Tony');
INSERT INTO `Members` (`Club_ID`, `Name`) VALUES ('5', 'Oscar');

这应该只返回 Club_ID =1!

4

3 回答 3

1
select Club_ID from Members group by Club_ID having count(*)>1
于 2013-06-03T21:57:48.850 回答
1
  SELECT COUNT(*), Club_ID
    FROM Members
GROUP BY Club_ID
  HAVING COUNT(*) > 1
于 2013-06-03T21:57:27.300 回答
0
select club_id
from members
group by club_id
having count(distinct id) >= 2
于 2013-06-03T21:57:28.800 回答