在JOIN查询中使用Match ...进行关联性计算(MySQL)

时间:2018-08-22 20:53:34

标签: mysql join relevance match-against

我正在尝试使用以下JOIN查询获得相关的搜索结果:

SELECT 
    `products`.`id`,
    `brands`.`name` AS `brand`,
    `products`.`name` AS `productname`,
    MATCH (`brands`.`name`) AGAINST ('somebrand someproduct' ) AS brandname_relevance
FROM
    `brands`
        INNER JOIN
    `products` ON `products`.`brand_id` = `brands`.`id`
WHERE
    MATCH (`products`.`name`) AGAINST ('somebrand someproduct' ) AS /* AS is highlighted as 'not valid input at this point' */ product_relevance
ORDER BY (brandname_relevance + product_relevance) DESC

但是,在此上下文中,MySQL Workbench将代码“ AS”突出显示为无效。查询失败。

两个表中的

Engine是InnoDB,但是MySQL服务器版本高于5.6,并且brands.nameproducts.name均启用了FULLTEXT索引。请在这里说明问题所在。

1 个答案:

答案 0 :(得分:1)

在where子句中-将其与您选择的其他字段一起向上移动。

SELECT 
    `products`.`id`,
    `brands`.`name` AS `brand`,
    `products`.`name` AS `productname`,
    MATCH (`brands`.`name`) AGAINST ('somebrand someproduct' ) AS brandname_relevance,
    MATCH (`products`.`name`) AGAINST ('somebrand someproduct' ) AS product_relevance
FROM
    `brands`
        INNER JOIN
    `products` ON `products`.`brand_id` = `brands`.`id`
ORDER BY (brandname_relevance + product_relevance) DESC