0

我想知道我的哪些用户在他们的机器上有一个独特的操作系统

我对不完全独特的人不感兴趣。(我们在这里寻找独狼)我不是在寻找独特的价值观。只是完全独特的记录。

这是SQL

CREATE  TABLE `Computers` (
  `ID` INT NOT NULL AUTO_INCREMENT ,
  `CPU` VARCHAR(45) NULL ,
  `OS` VARCHAR(45) NULL ,
  PRIMARY KEY (`ID`) );


INSERT INTO `Computers` (`CPU`, `OS`) VALUES ('386', 'win 3.1');
INSERT INTO `Computers` (`CPU`, `OS`) VALUES ('486', 'Dr Doss');
INSERT INTO `Computers` (`CPU`, `OS`) VALUES ('286', 'win 3.1');
INSERT INTO `Computers` (`CPU`, `OS`) VALUES ('68000', 'Mac OS 7.1');
INSERT INTO `Computers` (`CPU`, `OS`) VALUES ('68030', 'Mac OS 7.1');
INSERT INTO `Computers` (`CPU`, `OS`) VALUES ('Z80', 'CPM');

鉴于这些数据,我应该只取回以下行:

ID       OS
2        Dr Doss
6        CPM

有人可以帮我处理 SQL 语句吗?

4

4 回答 4

3

使用带有 的GROUP BY子句HAVING COUNT()

SELECT ID, OS
FROM Computers
GROUP BY OS
HAVING COUNT(OS) = 1;

GROUP BY子句将每个操作系统缩小到一行,并且该HAVING子句过滤掉表中具有多行的操作系统。

于 2013-07-01T21:22:22.377 回答
1

http://sqlfiddle.com/#!2/cbfc2/11

使用上面提供的示例

select min(`id`), `os`, count(*) as total
from computers
group by `os`
having `total` = 1
于 2013-07-01T21:24:29.397 回答
0

这个?

SELECT * FROM computers
GROUP BY OS
HAVING COUNT(ID) = 1

SQL 小提琴测试

于 2013-07-01T21:22:25.257 回答
0
SELECT *
FROM computers
INNER JOIN
(
SELECT OS, COUNT(*) AS OsCount
FROM computers
GROUP BY OS
HAVING OsCount = 1
) Sub1
ON computers.OS = Sub1.OS
于 2013-07-01T21:22:33.267 回答