将全文查询添加到现有查询

时间:2010-08-30 09:40:27

标签: mysql full-text-search

我正在努力找出使用单表全文加入普通多表查询的语法。我无法理解语法

这是现有的语法,可以正常工作:

SELECT * FROM products,categories,sub_categories 
WHERE products.approved = 1 
AND products.main_category = categories.id 
AND products.sub_category = sub_categories.id

我添加全文的努力并未削减它。这是我试图用于搜索的查询:

MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,details) 
AGAINST ('keyword') AS score FROM products 
WHERE MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,details) AGAINST('keyword')

这就是我想出来的,但是对于我不知道的(可能是显而易见的)原因,它不起作用。

SELECT * FROM products,categories,sub_categories 
WHERE products.approved = 1 
AND products.main_category = categories.id 
AND products.sub_category = sub_categories.id
AND MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,details) 
AGAINST ('keyword') AS score FROM products 
WHERE MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,details) AGAINST('keyword')

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

最低限度,你需要将“得分”移动到SELECT子句中,并将两个WHERE clases与AND组合。

SELECT *, 
  MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,details) 
  AGAINST ('keyword') AS score
FROM products,categories,sub_categories 
WHERE products.approved = 1 
  AND products.main_category = categories.id 
  AND products.sub_category = sub_categories.id
  AND MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,details) AGAINST('keyword')

我还强烈建议使用ANSI连接语法重写整个查询,以使其更具可读性:

SELECT *, 
  MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,details) 
  AGAINST ('keyword') AS score
FROM products
  INNER JOIN categories on products.main_category = categories.id
  INNER JOIN sub_categories on products.sub_category = sub_categories.id
WHERE products.approved = 1 
  AND MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,details) AGAINST('keyword')

答案 1 :(得分:0)

感谢大家。即时通讯与其他语言相当不错,但是带有mysql语法的flakey。老实说,当我看到你的代码时,事情就开始出现了!我可以看看你是如何得出这个结果的。我给它一个旋转,但看起来不错。

答案 2 :(得分:0)

我如何添加'order by product_name'?我试过这个,但结果不正确

SELECT * FROM products 
INNER JOIN product_categories on products.main_category = product_categories.id 
INNER JOIN product_sub_categories on products.sub_category = product_sub_categories.id 
WHERE products.approved = 1 AND products.main_category = 8 
ORDER BY products.product_name DESC LIMIT 0,25 

但是,当添加搜索时,这会起作用

SELECT * , 
MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,product_details) 
AGAINST ('keyword') AS score FROM my_products 
INNER JOIN my_product_categories on my_products.main_category = my_product_categories.id 
INNER JOIN my_product_sub_categories on my_products.sub_category = my_product_sub_categories.id 
WHERE my_products.approved = 1 AND 
MATCH(product_name,code,tag_1,tag_2,tag_3,tag_4,tag_5,product_details) 
AGAINST('bath') ORDER BY my_products.product_name ASC LIMIT 0,25