2014-12-04 164 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替换条件(也许实际数据不像示例)。