join查询(mysql)

r7knjye2  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(314)

我正在尝试使用以下联接查询获取相关搜索结果:

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”是无效的。查询失败。
两个表中的引擎都是innodb,但是mysql服务器版本都在5.6以上,并且都启用了全文索引 brands.name 以及 products.name . 请解释一下这里有什么问题。

l5tcr1uw

l5tcr1uw1#

在where子句中-将其与select中的其他字段一起上移。

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

相关问题