mysql:限制为多个查询的最大相等结果

时间:2012-11-21 19:53:12

标签: mysql

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 =年轻,term2 = home,term3 =蓝色

 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

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

3 个答案:

答案 0 :(得分:0)

子查询可以是执行此类操作的简单方法。你真正想要的是所有条目的价值(年龄='年轻')+(工作='家庭')+(眼睛='蓝& #39;)等于所有条目的最高值。

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
        )

答案 1 :(得分:0)

如果你想根据相似性得到结果,下面的内容会给你一个结果,但我必须编写所有的组合,所以在实际案例中你需要n! + 1 *条件。

  

n!代表“factorial”,例如 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   |

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

答案 2 :(得分: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')
 ;