我有一个搜索功能的SQL查询有点复杂,但效果很好。
__setattr__
现在它按产品标题排序,但它应该首先列出最相关的结果。这意味着,如果它与SELECT
SQL_CALC_FOUND_ROWS p.id as id,
p.title as title,
co.title as company,
p.price as price,
p.image_url as image_url FROM products p
JOIN product_categories pc ON p.id = pc.product_id
JOIN categories cat ON pc.category_id = cat.id
JOIN companies co ON p.company_id = co.id
WHERE MATCH(p.title) AGAINST("nalle" IN BOOLEAN MODE)
OR MATCH(p.description) AGAINST("nalle" IN BOOLEAN MODE)
OR MATCH(cat.title) AGAINST("nalle" IN BOOLEAN MODE)
OR MATCH(co.title) AGAINST("nalle" IN BOOLEAN MODE)
GROUP BY p.id
ORDER BY p.title
LIMIT :offset, :limit
和p.title
都匹配,则它比仅与p.description
匹配更相关。最好的匹配是匹配所有四个。
由于查询的复杂性,我不知道如何计算它。
我猜我需要p.title
... SELECT ??? as points
。
答案 0 :(得分:0)
如果它是 InnoDB ,也许你可以尝试一下,我对MyISAM不确定:
SELECT
SQL_CALC_FOUND_ROWS p.id as id,
p.title as title,
co.title as company,
p.price as price,
p.image_url as image_url,
MATCH(p.title, p.description, cat.title, co.title)
AGAINST("nalle" IN BOOLEAN MODE) as Relevance
FROM products p
JOIN product_categories pc ON p.id = pc.product_id
JOIN categories cat ON pc.category_id = cat.id
JOIN companies co ON p.company_id = co.id
WHERE MATCH(p.title, p.description, cat.title, co.title) AGAINST("nalle" IN BOOLEAN MODE)
GROUP BY p.id
ORDER BY Relevance DESC, p.title
LIMIT :offset, :limit
在示例here中,您可以看到他们如何计算得分。如果您要查找的单词在更多字段中找到,则分数会更高。