0
Name   Age     Work    Eyes
---------------------------
John   young  home   black

Mike  young   home   blue

Irvin   old   home   black

Marie  young  home   blue

Teddy  old   factory  green

多选框形式,搜索term1=young,term2=home,term3=blue

 SELECT   Name 
 FROM     my_table 
 WHERE    (Age = 'young')
       OR (Work = 'home')
       OR (Eyes = 'blue') 
 ORDER BY (Age = 'young')
        + (Work = 'home')
        + (Eyes = 'blue') DESC LIMIT 1

在我的情况下,两个结果具有最大相等的巧合(Mike 和 Marie),我想展示它们。
如何限制最常见的结果?可能只有一个或多个:2,3...同样的巧合
:感谢 eggyal 订购

4

3 回答 3

0

子查询可以是做这种事情的一种简单方法。您真正要查找的是所有条目,其中 (Age = 'young') + (Work = 'home') + (Eyes = 'blue') 的值等于所有条目中的最大值。

SELECT Name
FROM my_table
WHERE
    Age = 'young'
    OR Work = 'home'
    OR Eyes = 'blue'
    AND ( (Age = 'young') + (Work = 'home') + (Eyes = 'blue') ) = (
            SELECT MAX((Age = 'young') + (Work = 'home') + (Eyes = 'blue'))
            FROM my_table
        )
于 2012-11-21T21:20:51.370 回答
0

如果您想根据相似性获得结果,以下将为您提供结果,但我必须编写所有组合,因此在实际情况下您将需要有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   |

这不是非常实用,但我不知道如何通过任何其他方法获得名称组合。

于 2012-11-25T05:17:06.827 回答
0

我认为不扫描整个表(两次)就没有解决方案。这是一个:

SELECT t.Name
FROM 
    my_table AS t
  JOIN
    ( SELECT MAX((Age = 'young') + (Work = 'home') + (Eyes = 'blue')) AS matching
      FROM my_table
    ) AS m
    ON  m.matching > 0  
    AND m.matching = (t.Age = 'young') + (t.Work = 'home') + (t.Eyes = 'blue')
 ;
于 2012-11-25T08:53:14.840 回答