我如何找到两个具有特定名称的员工一起工作的项目数量? (使用 LIKE 运算符)
How do i find the number of projects that two employees with specific names that have worked together? (using LIKE operator )
假设我想查找 Jim Sullivan 和 Anna Schimdt 这两名员工共同从事的项目数量。我们有 3 个表 employees、workson、project。
员工
employeeid
name
110
Jim Sullivan
111
Anna Schimdt
112
James Lee
沃森
projectid
employeeid
6554
110
6554
111
6555
110
6555
111
6556
110
6556
111
6556
112
项目
projectid
projectName
6556
POPS
6555
BABY
6554
MAMA
从数据样本来看,Jim Sullivan 和 Anna Schimdt 一起从事过 3 个项目。因此预期结果应该是 3。由于某种原因,我的代码没有显示任何结果。这是我的代码:
SELECT COUNT(w.employeeid)
FROM workson w
JOIN employees e ON e.employeeid = w.employeeid
JOIN projects p ON p.projectid = w.projectid
WHERE name LIKE 'jim%sullivan%'
AND name LIKE 'anna%schmidt%';
出于某种原因,如果我只过滤掉一名员工,代码就可以工作,但一旦我包括了他们两个,代码就不起作用了。例如,如果我只有 Anna Schmidt,那么结果就会出来,但是一旦我添加了 AND 运算符,它就不起作用了。它确实适用于 OR 运算符
这将显示 Jim 参与的项目:
SELECT
p.projectid,
p.projectName
FROM Projects p
INNER JOIN Workson w ON w.projectid = p.projectid
INNER JOIN employees e ON e.employeeid = w.employeeid
WHERE e.name LIKE 'jim%sullivan%'
当你需要共享2个作品的项目时,你可以这样做:
SELECT
p.projectid,
p.projectName
FROM Projects p
INNER JOIN Workson w1 ON w1.projectid = p.projectid
INNER JOIN Workson w2 ON w2.projectid = p.projectid
INNER JOIN Employees e1 ON e1.employeeid = w1.employeeid
INNER JOIN Employees e2 ON e2.employeeid = w2.employeeid
WHERE e1.name LIKE 'jim%sullivan%'
AND e2.name LIKE 'anna%schmidt%';
编辑:我忘记了一个 JOIN,现在可以了,请参阅:DBFIDDLE
使用此查询:
SELECT w.projectid
FROM Workson w INNER JOIN Employees e
ON e.employeeid = w.employeeid
WHERE (e.name LIKE 'jim%sullivan%') OR (e.name LIKE 'anna%schmidt%')
GROUP BY w.projectid
HAVING COUNT(*) = 2;
你得到了 Jim Sullivan 和 Anna Schimdt(或 Schmidt)合作过的所有 projectid
s。
我不知道为什么您使用运算符 LIKE
而不是运算符 =
来将列 name
与员工姓名进行比较,所以我保持原样,但是这个例如,如果有另一个名为 'Annabelle Schmidt'.
的员工,则会导致问题
使用上面的查询作为子查询来统计行数:
SELECT COUNT(*) count
FROM (
SELECT w.projectid
FROM Workson w INNER JOIN Employees e
ON e.employeeid = w.employeeid
WHERE (e.name LIKE 'jim%sullivan%') OR (e.name LIKE 'anna%schmidt%')
GROUP BY w.projectid
HAVING COUNT(*) = 2
) t;
或者,对于 MySql 8.0+ 使用 COUNT()
window 函数:
SELECT DISTINCT COUNT(*) OVER () count
FROM Workson w INNER JOIN Employees e
ON e.employeeid = w.employeeid
WHERE (e.name LIKE 'jim%sullivan%') OR (e.name LIKE 'anna%schmidt%')
GROUP BY w.projectid
HAVING COUNT(*) = 2;
参见demo。
不需要tableProjects
假设我想查找 Jim Sullivan 和 Anna Schimdt 这两名员工共同从事的项目数量。我们有 3 个表 employees、workson、project。
员工
employeeid | name |
---|---|
110 | Jim Sullivan |
111 | Anna Schimdt |
112 | James Lee |
沃森
projectid | employeeid |
---|---|
6554 | 110 |
6554 | 111 |
6555 | 110 |
6555 | 111 |
6556 | 110 |
6556 | 111 |
6556 | 112 |
项目
projectid | projectName |
---|---|
6556 | POPS |
6555 | BABY |
6554 | MAMA |
从数据样本来看,Jim Sullivan 和 Anna Schimdt 一起从事过 3 个项目。因此预期结果应该是 3。由于某种原因,我的代码没有显示任何结果。这是我的代码:
SELECT COUNT(w.employeeid)
FROM workson w
JOIN employees e ON e.employeeid = w.employeeid
JOIN projects p ON p.projectid = w.projectid
WHERE name LIKE 'jim%sullivan%'
AND name LIKE 'anna%schmidt%';
出于某种原因,如果我只过滤掉一名员工,代码就可以工作,但一旦我包括了他们两个,代码就不起作用了。例如,如果我只有 Anna Schmidt,那么结果就会出来,但是一旦我添加了 AND 运算符,它就不起作用了。它确实适用于 OR 运算符
这将显示 Jim 参与的项目:
SELECT
p.projectid,
p.projectName
FROM Projects p
INNER JOIN Workson w ON w.projectid = p.projectid
INNER JOIN employees e ON e.employeeid = w.employeeid
WHERE e.name LIKE 'jim%sullivan%'
当你需要共享2个作品的项目时,你可以这样做:
SELECT
p.projectid,
p.projectName
FROM Projects p
INNER JOIN Workson w1 ON w1.projectid = p.projectid
INNER JOIN Workson w2 ON w2.projectid = p.projectid
INNER JOIN Employees e1 ON e1.employeeid = w1.employeeid
INNER JOIN Employees e2 ON e2.employeeid = w2.employeeid
WHERE e1.name LIKE 'jim%sullivan%'
AND e2.name LIKE 'anna%schmidt%';
编辑:我忘记了一个 JOIN,现在可以了,请参阅:DBFIDDLE
使用此查询:
SELECT w.projectid
FROM Workson w INNER JOIN Employees e
ON e.employeeid = w.employeeid
WHERE (e.name LIKE 'jim%sullivan%') OR (e.name LIKE 'anna%schmidt%')
GROUP BY w.projectid
HAVING COUNT(*) = 2;
你得到了 Jim Sullivan 和 Anna Schimdt(或 Schmidt)合作过的所有 projectid
s。
我不知道为什么您使用运算符 LIKE
而不是运算符 =
来将列 name
与员工姓名进行比较,所以我保持原样,但是这个例如,如果有另一个名为 'Annabelle Schmidt'.
使用上面的查询作为子查询来统计行数:
SELECT COUNT(*) count
FROM (
SELECT w.projectid
FROM Workson w INNER JOIN Employees e
ON e.employeeid = w.employeeid
WHERE (e.name LIKE 'jim%sullivan%') OR (e.name LIKE 'anna%schmidt%')
GROUP BY w.projectid
HAVING COUNT(*) = 2
) t;
或者,对于 MySql 8.0+ 使用 COUNT()
window 函数:
SELECT DISTINCT COUNT(*) OVER () count
FROM Workson w INNER JOIN Employees e
ON e.employeeid = w.employeeid
WHERE (e.name LIKE 'jim%sullivan%') OR (e.name LIKE 'anna%schmidt%')
GROUP BY w.projectid
HAVING COUNT(*) = 2;
参见demo。
不需要tableProjects