如果您想根据相似性获得结果,以下将为您提供结果,但我必须编写所有组合,因此在实际情况下您将需要有n! + 1
* 条件。
嗯!代表“阶乘”,例如3! 表示3 * 2 * 1 = 6
和+1是整个比赛(在你的情况下为 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 |
这不是非常实用,但我不知道如何通过任何其他方法获得名称组合。