如果你想獲得基於相似的結果,下面會給你一個結果,但我不得不所有組合寫那麼在現實的情況下,你需要有n! + 1
*條件。
n!代表「階乘」,例如3!裝置3 * 2 * 1 = 6
和是整個比賽(3中的情況下)
SELECT
GROUP_CONCAT(`Name` SEPARATOR ' - ') AS 'Names',
COUNT(*) AS 'matches',
1 AS '_Age',1 AS '_Work',1 AS '_Eyes'
FROM `my_table` WHERE
`Age`='young' OR
`Work`='home' OR
`Eyes`='blue'
GROUP BY `Age`,`Work`,`Eyes`
HAVING `matches` > 1
UNION
SELECT
GROUP_CONCAT(`Name` SEPARATOR ' - ') AS 'Names',
COUNT(*) AS 'matches',
1 AS '_Age',1 AS '_Work',0 AS '_Eyes'
FROM `my_table` WHERE
`Age`='young' OR
`Work`='home' OR
`Eyes`='blue'
GROUP BY `Age`,`Work`
HAVING `matches` > 1
UNION
SELECT
GROUP_CONCAT(`Name` SEPARATOR ' - ') AS 'Names',
COUNT(*) AS 'matches',
1 AS '_Age',0 AS '_Work',1 AS '_Eyes'
FROM `my_table` WHERE
`Age`='young' OR
`Work`='home' OR
`Eyes`='blue'
GROUP BY `Age`,`Eyes`
HAVING `matches` > 1
UNION
SELECT
GROUP_CONCAT(`Name` SEPARATOR ' - ') AS 'Names',
COUNT(*) AS 'matches',
0 AS '_Age',1 AS '_Work',1 AS '_Eyes'
FROM `my_table` WHERE
`Age`='young' OR
`Work`='home' OR
`Eyes`='blue'
GROUP BY `Work`,`Eyes`
HAVING `matches` > 1
UNION
SELECT
GROUP_CONCAT(`Name` SEPARATOR ' - ') AS 'Names',
COUNT(*) AS 'matches',
1 AS '_Age',0 AS '_Work',0 AS '_Eyes'
FROM `my_table` WHERE
`Age`='young' OR
`Work`='home' OR
`Eyes`='blue'
GROUP BY `Age`
HAVING `matches` > 1
UNION
SELECT
GROUP_CONCAT(`Name` SEPARATOR ' - ') AS 'Names',
COUNT(*) AS 'matches',
0 AS '_Age',1 AS '_Work',0 AS '_Eyes'
FROM `my_table` WHERE
`Age`='young' OR
`Work`='home' OR
`Eyes`='blue'
GROUP BY `Work`
HAVING `matches` > 1
UNION
SELECT
GROUP_CONCAT(`Name` SEPARATOR ' - ') AS 'Names',
COUNT(*) AS 'matches',
0 AS '_Age',0 AS '_Work',1 AS '_Eyes'
FROM `my_table` WHERE
`Age`='young' OR
`Work`='home' OR
`Eyes`='blue'
GROUP BY `Eyes`
HAVING `matches` > 1
ORDER BY
`_Age`+`_Work`+`_Eyes` DESC,
`matches` DESC
而結果將是
| Names | matches | _Age | _Work | _Eyes |
+-----------------------------+---------+------+-------+-------+
| Mike - Marie | 2 | 1 | 1 | 1 |
| John - Mike - Marie | 3 | 1 | 1 | 0 |
| Mike - Marie | 2 | 1 | 0 | 1 |
| John - Irvin | 2 | 0 | 1 | 1 |
| Mike - Marie | 2 | 0 | 1 | 1 |
| John - Mike - Irvin - Marie | 4 | 0 | 1 | 0 |
| John - Mike - Marie | 3 | 1 | 0 | 0 |
| Mike - Marie | 2 | 0 | 0 | 1 |
| John - Irvin | 2 | 0 | 0 | 1 |
這不是非常實用,但我不知道如何通過任何其他方法獲得名稱組合。
作品很好,謝謝 – Telu