2012-04-04 106 views
2

由於某種原因,以下查詢允許重複的名稱。這是爲什麼?sql查詢未正確分組

SELECT id, name_without_variants, SUM(relevance) as total_relevance FROM (
    SELECT 
     card_definitions.id, 
      card_definitions.name_without_variants, 
     (MATCH(card_definitions.name_without_variants) AGAINST ('lost soul site discard')) * 0.40 AS relevance 
     FROM card_definitions 
     GROUP BY name_without_variants, id 
    UNION 
    SELECT 
     card_definitions.id, 
      card_definitions.name_without_variants, 
     (MATCH(card_def_identities.special_ability_text) AGAINST ('lost soul site discard')) * 0.05 AS relevance 
     FROM card_def_identities 
     INNER JOIN card_definitions ON card_def_identities.card_def_sid = card_definitions.id 
     GROUP BY name_without_variants, id 
    UNION 
    SELECT 
     card_definitions.id, 
      card_definitions.name_without_variants, 
     (MATCH(brigades.brigade_color) AGAINST ('lost soul site discard')) * 0.30 AS relevance 
     FROM brigades 
     INNER JOIN card_def_brigades ON brigades.id = card_def_brigades.brigade_sid 
     INNER JOIN card_definitions ON card_def_brigades.card_def_sid = card_definitions.id 
     GROUP BY name_without_variants, id 
    UNION 
    SELECT 
     card_definitions.id, 
      card_definitions.name_without_variants, 
     (MATCH(identifiers.identifier) AGAINST ('lost soul site discard')) * 0.20 AS relevance 
     FROM identifiers 
     INNER JOIN card_def_identifiers ON identifiers.id = card_def_identifiers.identifier_sid 
     INNER JOIN card_definitions on card_def_identifiers.card_def_sid = card_definitions.id 
     GROUP BY name_without_variants, id 
    UNION 
    SELECT 
     card_definitions.id, 
      card_definitions.name_without_variants, 
     (MATCH(card_effects.effect) AGAINST ('lost soul site discard')) * 0.05 AS relevance 
     FROM card_effects 
     INNER JOIN card_def_effects ON card_effects.id = card_def_effects.effect_sid 
     INNER JOIN card_definitions on card_def_effects.card_def_sid = card_definitions.id 
     GROUP BY name_without_variants, id 
    ) AS combined_search 
GROUP BY name_without_variants, id 
HAVING total_relevance > 0 
ORDER BY total_relevance DESC 
LIMIT 10; 

這是我得到的結果。注意兩個Lost Soul [Site Doubler]

2623 Lost Soul [Deck Discard] 6.35151714086533 
1410 Lost Soul [Hand Discard] 6.29273346662521 
1495 Lost Soul [Discard Card] 5.93360201716423 
1442 Lost Soul [Demon Discard] 5.91308708190918 
1497 Lost Soul [Site Doubler] 5.05888686180115 
1498 Lost Soul [Site Doubler] 5.05888686180115 
2572 Lost Soul [Site Guard] 4.82421946525574 
2774 Lost Soul [Far Country] 3.39325473308563 
2891 Fortify Site [RoA2] 2.77084048986435 
1418 Lost Soul [Hopper] 2.63041100502014 

回答

2

因爲ID是不同的,你是ID分組,你多行的每個,這就是GROUP BY一樣。如果您改變了頂層SELECT

SELECT name_without_variants, SUM(relevance) as total_relevance 

和外GROUP BY到:

GROUP BY name_without_variants 

你應該看到不同的名稱,但將不再有ID。

+0

如果您將其從group by語句中刪除,則還必須向id列添加聚合函數。你將需要最小/最大或類似的東西。 – 2012-04-04 17:24:41

0
GROUP BY name_without_variants, id 

您正在按name_without_variants,id進行分組。該id在兩條記錄上有所不同:

1497 Lost Soul [Site Doubler] 5.05888686180115 
1498 Lost Soul [Site Doubler] 5.05888686180115 

您需要決定如何管理id。

從組中刪除id,然後將聚合函數添加到select中的id列。或者只是一起刪除列。

下面是一個簡化爲單個查詢的示例。請理解我對您的架構或數據沒有全面的看法,也沒有經過測試。我也在這裏做一些假設。但是,如果架構是關係型的,這應該帶回您正在尋找的內容:

SELECT cd.id, cd.name_without_variants, (((MATCH(cd.name_without_variants) AGAINST ('lost soul site discard')) * 0.40)+ 
            ((MATCH(cdi.special_ability_text) AGAINST ('lost soul site discard')) * 0.05)+ 
            ((MATCH(b.brigade_color) AGAINST ('lost soul site discard')) * 0.30)+ 
            ((MATCH(i.identifier) AGAINST ('lost soul site discard')) * 0.20)+ 
            ((MATCH(ce.effect) AGAINST ('lost soul site discard')) * 0.05) 
           ) as total_relevance 
FROM card_definitions cd 
LEFT OUTER JOIN card_def_identities cdi ON cd.id=cdi.card_def_sid 
LEFT OUTER JOIN brigades b ON cd.id=b.card_def_sid 
LEFT OUTER JOIN identifiers i ON i.id=cdi.identifier_sid 
LEFT OUTER JOIN card_def_effects cde ON cde.card_def_sid=cd.id 
LEFT OUTER JOIN card_effects ce ON ce.id=cde.effect_sid 
GROUP BY cd.id, cd.name_without_variants 
HAVING total_relevance > 0 
ORDER BY total_relevance DESC 
LIMIT 10; 
+0

哦,我認爲小組會按名稱分組,然後如果還有任何重複項,請按id將它們分組。儘管我仍然需要身份證。我怎樣才能得到1的ID,並確保沒有重複的名稱? – LordZardeck 2012-04-04 17:33:28

+0

您必須在ID列上使用最小/最大值。然而,那麼你會從聯合選擇語句中得到一個任意的id。從頂級選擇中刪除ID可能是明智的,因爲它沒有多大意義。否則,你將不得不在你的union select語句中添加一個靜態列,以便你知道id來自哪個語句。合理?如果需要,我可以編輯顯示的答案。 – 2012-04-04 17:38:52

+0

我需要該ID。這是我實際使用的唯一的東西。如果你不介意,我能看到一個例子嗎?我不知道任何關於最小/最大 – LordZardeck 2012-04-04 17:45:25