2014-12-04 160 views
0

我在我的分貝中有成分表。我想獲取那些含有一些成分但不含某些成分的食譜。全文搜索查詢mysql

例如,我有兩個食譜都包含「pav」,但其中一個不包含「phudina」。現在,下面是我的數據庫表數據。

Here in table(ingredients) recipeID 24(rcteID) contains pav and phudina 
----------------------------------------------------------------------- 
ingredientID rcteID ingredient 
    319   24  phudina 
    320   24  pav  

Here in table(ingredients) recipeID 23(rcteID) not contains phudina 
----------------------------------------------------------------------- 
ingredientID rcteID ingredient 
    316   23  test  
    317   23  pav  
    318   23  puri  

現在我希望這些配方IDS包含「PAV」,而不是「phudina」所以,在這裏它是23,但我的查詢是給我兩個食譜。我已經在成分欄中應用了全文索引。

Below is the query which I have written. 

SELECT `Ingredient`.`ingredientID`, `Ingredient`.`rcteID` 
FROM `bdrplus`.`ingredient` AS `Ingredient` 
WHERE NOT(MATCH(`Ingredient`.`ingredient`) AGAINST('+phudina' IN BOOLEAN MODE)) 
AND MATCH(`Ingredient`.`ingredient`) AGAINST('+pav' IN BOOLEAN MODE) 
GROUP BY `Ingredient`.`rcteID 

    Output 
    ---------------------------- 
     ingredientID rcteID 
      317   23 
      320   24 
    Expected 
    ----------------------------- 
     ingredientID rcteID 
      317   23 

查詢有什麼問題?

+0

爲什麼在只包含單個單詞的列上使用全文搜索? – Barmar 2014-12-04 10:17:59

回答

1

您正在測試每個行的兩個條件,而不是查看具有相同rcteID的其他行。如果您將所有成分放在一個字符串中,那麼這將起作用,但當您對錶格進行規範化時,它不起作用。使用此:

SELECT ingredientID, rcteID 
FROM ingredient 
WHERE ingredient = 'pav' 
AND rcteID NOT IN (
    SELECT rcteID 
    FROM ingredient 
    WHERE ingredient = 'phudina' 
) 

DEMO

也似乎有不被需要使用全文搜索時ingredient列只是一個字。但是,如果您確實需要=條件,則可以用MATCH替換條件(也許實際數據不像示例)。