如何区分数据库中多次出现的元素

How to discriminate elements from database that have many occurences

我有一个名为 ProjectRessources 的 table,其中包含以下数据:

+-----------+------------+---------+-----------+
| projectId | employeeId | nbHours | wageHours |
+-----------+------------+---------+-----------+
| 1         | 1876       | 500     | 65        |
| 1         | 4354       | 2000    | 31        |
| 2         | 2231       | 250     | 55        |
| 3         | 1212       | 3000    | 35        |
| 3         | 1876       | 2000    | 35        |
| 3         | 2231       | 500     | 65   

我必须查询从事过多个项目的员工才能获得这样的信息:

+------------+----------------+
| employeeId | nbProjects |
+------------+----------------+
| 1876       | 2              |
| 2231       | 2              |
+------------+----------------+

我试过了

    SELECT DISTINCT employeeID,projectID
    FROM ProjectRessources
    WHERE COUNT(projectID) >1;

但我在使用 COUNT

时收到错误 1111
ERROR 1111 (HY000): Invalid use of group function

然后我尝试了这样的事情

SELECT DISTINCT
employeeId ,
COUNT(projectId) as nbProjects
From ProjectRessources
WHERE nbProjects >1;

但是我得到以下错误

ERROR 1054 (42S22): Unknown column 'nbProjects' in 'where clause'

请帮忙,欢迎任何反馈。请注意,不允许我使用 GROUP BY 进行查询。

你想要having。 . .和右边 group by:

SELECT employeeID
FROM ProjectRessources
GROUP BY employeeId
HAVING COUNT(*) > 1;

如果你想要项目列表,我会推荐GROUP_CONCAT():

SELECT employeeID, GROUP_CONCAT(projectID) as projectIDs
FROM ProjectRessources
GROUP BY employeeId
HAVING COUNT(*) > 1;

您可以尝试加入 ProjectResources table 与自身,如下所示:

select distinct p1.employeeID from
ProjectResources p1 join ProjectResources p2
    on p1.employeeID = p2.employeeID and
       p1.projectID <> p2.projectID

这只是尝试查看每一行是否有另一行具有相同的 employeeID 值但具有不同的 projectID 值。我们不在乎有多少,只要至少有一个,这就是为什么我们 select distinct 这样相同的 employeeID 不会出现超过一次(没有 distinct 关键字,我们会为员工从事的每个项目获得一行)。

我使用你更新后的问题中的 table 在 sqlfiddle.com 处创建了一个实际的 table。下次你可以(并且应该)自己做这个并且 post 你问题中的 link。

See Demo

但这听起来很像家庭作业。

如果你一定要数:

select distinct employeeID, count(*) OVER(PARTITION By employeeID) as nbProjects from (
   select p1.employeeID from
   ProjectResources p1 join ProjectResources p2
       on p1.employeeID = p2.employeeID and
          p1.projectID <> p2.projectID
  ) sq;
employeeID nbProjects
1876 2
2231 2

View on DB Fiddle

没有GROUP BY,你可以用相关的子查询来完成:

SELECT *
FROM (
  SELECT DISTINCT pr.employeeId,
         (SELECT COUNT(*) FROM ProjectRessources pr2 WHERE pr2.employeeId = pr.employeeId) nbProjects 
  FROM ProjectRessources pr
) t
WHERE nbProjects > 1

如果每个 employeeId 更改 COUNT(*) 有重复 projectId 的情况:

SELECT COUNT(DISTINCT projectId) FROM ...

如果您的 MySql 版本是 8.0+,并且每个 employeeId 没有重复的 projectId,您可以使用 COUNT(*) window 函数:

SELECT *
FROM (
  SELECT DISTINCT employeeId,
         COUNT(*) OVER (PARTITION BY employeeId) nbProjects 
  FROM ProjectRessources pr
) t
WHERE nbProjects > 1

参见demo
结果:

> employeeId | nbProjects
> ---------: | ---------:
>       1876 |          2
>       2231 |          2