鉴于以下数据:
EMPID NAME SALARY DID
1 kevin 32000 2
2 joan 42000 1
3 brian 37000 3
4 larry 82000 5
5 harry 92000 4
6 peter 45000 2
7 peter 68000 3
8 smith 39000 4
9 chen 71000 1
10 kim 46000 5
11 smith 46000 1
列出不具有相同名称的员工的姓名。
我不确定我是否必须自己加入桌子(或做其他事情)。
这是我到目前为止写的:
SELECT distinct e.name
FROM employee e, employee ee
WHERE e.name <> ee.name
答案 0 :(得分:5)
无需加入:
SELECT name
FROM employee
GROUP BY name
HAVING COUNT(name) = 1
计算每个名称的数量,然后只返回只有一个名称的名称。例如peter的数量为2,不会出现,而kim的数量为1并且会出现。
答案 1 :(得分:0)
如果您想获得不同的名称,以下查询就足够了:
SELECT distinct e.name
FROM employee e
希望它有所帮助!!!
答案 2 :(得分:0)
mysql> select * from employee;
+-------+-------+--------+-----+
| empid | name | salary | did |
+-------+-------+--------+-----+
| 1 | kevin | 32000 | 2 |
| 2 | joan | 42000 | 1 |
| 3 | brian | 37000 | 3 |
| 4 | larry | 82000 | 5 |
| 5 | harry | 92000 | 4 |
| 6 | peter | 45000 | 2 |
| 7 | peter | 68000 | 3 |
| 8 | smith | 39000 | 4 |
| 9 | chen | 71000 | 1 |
| 10 | kim | 46000 | 5 |
| 11 | smith | 46000 | 1 |
+-------+-------+--------+-----+
11 rows in set (0.00 sec)
mysql> select distinct name from employee;
+-------+
| name |
+-------+
| kevin |
| joan |
| brian |
| larry |
| harry |
| peter |
| smith |
| chen |
| kim |
+-------+
9 rows in set (0.00 sec)
答案 3 :(得分:0)
使用内连接计算每个emp的记录数。你可以过滤count = 1的结果
SELECT
T.EMPID,
T.NAME,
T.SALARY,
T.DID
FROM
employee AS T
INNER JOIN (SELECT COUNT(*) AS CNT , NAME FROM employee GROUP BY NAME) AS A ON T.NAME = A.NAME
WHERE
CNT = 1