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

Multiple checkbox form, search 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

In my case two results are with maximum-equal coincidences (Mike and Marie) I want to show both of them.
How to LIMIT for most frequently results? Could be only one or many: 2,3...equal coincidences
off:thanks eggyal for ordering

4

3 に答える 3

0

類似性に基づいて結果を取得したい場合は、次の結果が得られますが、すべての組み合わせを作成する必要があったため、実際のシナリオでは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   |

これは非常に実用的ではありませんが、他の方法で名前の組み合わせを取得する方法がわかりません。

于 2012-11-25T05:17:06.827 に答える
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

テーブル全体を(2回)スキャンしないと解決策はないと思います。ここに1つあります:

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 に答える