類似性に基づいて結果を取得したい場合は、次の結果が得られますが、すべての組み合わせを作成する必要があったため、実際のシナリオではn! + 1
*条件が必要になります。
n!「階乗」の略で、たとえば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 |
これは非常に実用的ではありませんが、他の方法で名前の組み合わせを取得する方法がわかりません。