2012-11-21 52 views
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 

多選框形式,搜索字詞1 =年輕,詞條2 =家,TERM3 =藍色
MySQL的:限於多個查詢

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訂購

回答

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') 
; 
+0

作品很好,謝謝 – Telu

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 
     ) 
+0

不完全是我想要的。在上面的表格中,我希望只有2個搜索結果(Mike和Marie)。在下一次搜索ex的另一個案例中。 「年輕」,「工廠」,「綠色」的搜索結果必須只有一個:「泰迪」;另一種情況是「年輕」,「家」。 「綠色」必須只有3排:Jonh,Mike,Marie。感謝和抱歉我的英語 – Telu

+0

我認爲這個答案是正確的,只有括號是錯誤的(年齡='年輕'或工作='家'或眼睛='藍')和(...)'將工作 – fthiella

0

如果你想獲得基於相似的結果,下面會給你一個結果,但我不得不所有組合寫那麼在現實的情況下,你需要有n! + 1 *條件。

n!代表「階乘」,例如3!裝置3 * 2 * 1 = 6
和是整個比賽(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 | 

這不是非常實用,但我不知道如何通過任何其他方法獲得名稱組合。