mysql "and" 结果集中的逻辑
mysql "and" logic within result set
假设我有如下数据集:
table foo
id | employeeType | employeeID
-------------------------
1 | Developer | 1
2 | Developer | 2
3 | Developer | 3
4 | Manager | 1
5 | Manager | 4
6 | Manager | 5
7 | CEO | 1
8 | CEO | 6
我想 运行 一个查询,该查询将 return 所有 employeeids(以及 employeeTypes),其中所有 employeeTypes 之间有一个共同的员工 id(即 'and'逻辑。只有具有所有 employeeType 的 employeeID 才会 return。employeeType = Developer and employeeType=Manager and employeeType=CEO)。对于上面的数据,示例输出将是
result table
id | employeeType | employeeID
-------------------------
1 | Developer | 1
4 | Manager | 1
7 | CEO | 1
当我只有两个 employeeTypes 时,我可以通过像这样自己加入 table 来做到这一点。
select * from foo as fooOne
join foo as fooTwo
on fooOne.employeeID = fooTwo.employeeID
AND
fooOne.employeeType <> fooTwo.employeeType
当 'and' 逻辑匹配时,查询 return 的结果集包含来自 fooTwo 的值,但同样,仅适用于两种类型的员工。我的真实用例场景表明我需要能够处理可变数量的员工类型(3、4、5 等...)
如有任何想法,我们将不胜感激。
我同意这可能会被视为一种非常 inefficient/hacky 的做事方式,但这仍然可以完成工作。坦率地说,我看不到任何其他解决方法。
SELECT * FROM (
SELECT EMPLOYEE_ID, GROUP_CONCAT(DISTINCT EmployeeType ORDER BY EmployeeType) AS Roles
FROM EMPLOYEES GROUP BY EMPLOYEE_ID
) EMPLOYEE_ROLES
WHERE EMPLOYEE_ROLES.Roles = 'CEO,Developer,Manager';
请注意,最后提供的以逗号分隔的角色列表是按字母顺序排列的。
您可以尝试使用子查询使其动态化
SELECT employeeID, employeeType
FROM foo
WHERE employeeID IN (
SELECT employeeID
FROM foo
GROUP BY employeeID
HAVING COUNT(DISTINCT employeeType) = (SELECT COUNT(DISTINCT employeeType) FROM foo)
)
这应该 return 您想要的行:
SELECT foo.*
FROM
foo
WHERE
employeeID IN (
SELECT employeeID
FROM foo
GROUP BY employeeID
HAVING COUNT(DISTINCT employeeType) =
(SELECT COUNT(DISTINCT employeeType)
FROM foo)
)
请看fiddle here.
内部查询将 return 不同员工类型的数量:
(SELECT COUNT(DISTINCT employeeType) FROM foo)
中间查询将return所有员工类型数量最多的员工ID:
SELECT employeeID
FROM foo
GROUP BY employeeID
HAVING COUNT(DISTINCT employeeType) =
(SELECT COUNT(DISTINCT employeeType) FROM foo)
并且外部查询将 return 整行。
假设我有如下数据集:
table foo
id | employeeType | employeeID
-------------------------
1 | Developer | 1
2 | Developer | 2
3 | Developer | 3
4 | Manager | 1
5 | Manager | 4
6 | Manager | 5
7 | CEO | 1
8 | CEO | 6
我想 运行 一个查询,该查询将 return 所有 employeeids(以及 employeeTypes),其中所有 employeeTypes 之间有一个共同的员工 id(即 'and'逻辑。只有具有所有 employeeType 的 employeeID 才会 return。employeeType = Developer and employeeType=Manager and employeeType=CEO)。对于上面的数据,示例输出将是
result table
id | employeeType | employeeID
-------------------------
1 | Developer | 1
4 | Manager | 1
7 | CEO | 1
当我只有两个 employeeTypes 时,我可以通过像这样自己加入 table 来做到这一点。
select * from foo as fooOne
join foo as fooTwo
on fooOne.employeeID = fooTwo.employeeID
AND
fooOne.employeeType <> fooTwo.employeeType
当 'and' 逻辑匹配时,查询 return 的结果集包含来自 fooTwo 的值,但同样,仅适用于两种类型的员工。我的真实用例场景表明我需要能够处理可变数量的员工类型(3、4、5 等...)
如有任何想法,我们将不胜感激。
我同意这可能会被视为一种非常 inefficient/hacky 的做事方式,但这仍然可以完成工作。坦率地说,我看不到任何其他解决方法。
SELECT * FROM (
SELECT EMPLOYEE_ID, GROUP_CONCAT(DISTINCT EmployeeType ORDER BY EmployeeType) AS Roles
FROM EMPLOYEES GROUP BY EMPLOYEE_ID
) EMPLOYEE_ROLES
WHERE EMPLOYEE_ROLES.Roles = 'CEO,Developer,Manager';
请注意,最后提供的以逗号分隔的角色列表是按字母顺序排列的。
您可以尝试使用子查询使其动态化
SELECT employeeID, employeeType
FROM foo
WHERE employeeID IN (
SELECT employeeID
FROM foo
GROUP BY employeeID
HAVING COUNT(DISTINCT employeeType) = (SELECT COUNT(DISTINCT employeeType) FROM foo)
)
这应该 return 您想要的行:
SELECT foo.*
FROM
foo
WHERE
employeeID IN (
SELECT employeeID
FROM foo
GROUP BY employeeID
HAVING COUNT(DISTINCT employeeType) =
(SELECT COUNT(DISTINCT employeeType)
FROM foo)
)
请看fiddle here.
内部查询将 return 不同员工类型的数量:
(SELECT COUNT(DISTINCT employeeType) FROM foo)
中间查询将return所有员工类型数量最多的员工ID:
SELECT employeeID
FROM foo
GROUP BY employeeID
HAVING COUNT(DISTINCT employeeType) =
(SELECT COUNT(DISTINCT employeeType) FROM foo)
并且外部查询将 return 整行。