我在MATCH()中尝试了CASE WHEN,但它给出了语法错误

时间:2015-10-16 09:56:51

标签: php mysql

 SELECT    products_description.products_title,
          product_settings.setting_value,
          products_description.product_description,
          products_description.long_description,
          products_additional_option_description.title,
          products_sizes.size_title,
          product_category_description.category_name,
          products.products_id,
          products_description.imagename,
          Match (products_description.products_title) against ('bu*'          IN boolean mode) AS rel_products_description_products_title,
          match (products_description.product_description) against ('bu*'     IN boolean mode) AS rel_products_description_product_description,
          match (products_description.long_description) against ('bu*'        IN boolean mode) AS rel_products_description_long_description,
          match (product_settings.setting_value) against ('bu*'               IN boolean mode) AS rel_product_settings_setting_value,
          match (products_additional_option_description.title) against ('bu*' IN boolean mode) AS rel_products_additional_option_description_title,
          match (products_sizes.size_title) against ('bu*'                    IN boolean mode) AS rel_products_sizes_size_title,
          match (product_category_description.category_name) against ('bu*'   IN boolean mode) AS rel_product_category_description_category_name ,
          CASE
                    WHEN store_products_description.products_title IS NOT NULL
                    AND       store_products_description.products_title!='' THEN store_products_description.products_title
                    ELSE products_description.products_title
          END AS products_title,
          CASE
                    WHEN store_products_description.product_description IS NOT NULL
                    AND       store_products_description.product_description!='' THEN store_products_description.product_description
                    ELSE products_description.product_description
          END AS product_description,
          CASE
                    WHEN store_products_description.long_description IS NOT NULL
                    AND       store_products_description.long_description!='' THEN store_products_description.long_description
                    ELSE products_description.long_description
          END AS long_description ,
          store_products.store_products_id
FROM      products
LEFT JOIN products_description
ON        products_description.products_id = products.products_id
LEFT JOIN store_products
ON        store_products.products_id = products.products_id
LEFT JOIN store_products_description
ON        store_products.store_products_id = store_products_description.store_products_id
AND       products_description.site_language_id = store_products_description.site_language_id
LEFT JOIN products_additional_option
ON        products_additional_option.products_id = products.products_id
LEFT JOIN products_additional_option_description
ON        products_additional_option_description.prod_add_opt_id = products_additional_option.prod_add_opt_id
LEFT JOIN product_settings
ON        product_settings.products_id = products.products_id
AND       product_settings.setting_constant_name = 'PRODUCT_SKU_NUMBER'
LEFT JOIN product_category
ON        product_category.category_id = products.category_id
LEFT JOIN product_category_description
ON        product_category.category_id = product_category_description.category_id
LEFT JOIN products_sizes
ON        products_sizes.products_id = products.products_id
WHERE     1
AND       products.visible = '1'
AND       store_products.visible = '1'
AND       products.products_id != '-11'
AND       (
                    store_products.user_type_id = '1'
          OR        store_products.user_type_id = '0' )
AND       products_description.site_language_id = '1'
AND       store_products_description.site_language_id = '1'
AND       (
                    match (
                    CASE
                              WHEN store_products_description.products_title IS NOT NULL
                              AND       store_products_description.products_title!='' THEN store_products_description.products_title
                              ELSE products_description.products_title
                    END AS products_title) against ('bu*' IN boolean mode)
          OR        match (
                    CASE
                              WHEN store_products_description.product_description IS NOT NULL
                              AND       store_products_description.product_description!='' THEN store_products_description.product_description
                              ELSE products_description.product_description
                    END AS product_description) against ('bu*' IN boolean mode)
          OR        match (
                    CASE
                              WHEN store_products_description.long_description IS NOT NULL
                              AND       store_products_description.long_description!='' THEN store_products_description.long_description
                              ELSE products_description.long_description
                    END AS long_description) against ('bu*'                             IN boolean mode)
          OR        match (product_settings.setting_value) against ('bu*'               IN boolean mode)
          OR        match (products_additional_option_description.title) against ('bu*' IN boolean mode)
          OR        match (products_sizes.size_title) against ('bu*'                    IN boolean mode)
          OR        match (product_category_description.category_name) against ('bu*'   IN boolean mode))
AND       store_products.store_id = '3'
GROUP BY  products.products_id
ORDER BY  (rel_products_description_products_title*7) +(rel_product_settings_setting_value*6) +(rel_products_description_product_description*5) +(rel_products_description_long_description*4) +(rel_products_additional_option_description_title*3) +(rel_products_sizes_size_title*2) +(rel_product_category_description_category_name*1) DESC

错误:#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以便在'(第6行的情况下使用商店_产品说明。产品编号')中使用正确的语法

2 个答案:

答案 0 :(得分:1)

您会混淆标识符和表达式。

MATCH() takes a comma-separated list that names the columns to be searched

CASE WHEN expression THEN column是一个表达式,当计算的表达式为true时,它返回列的

MATCH()看起来像一个函数,但它没有评估每一行的列值,因为它似乎正在做。它使用列名作为标识符传递给最完整的引擎。

编写查询的逻辑上正确的方法是相对于上面编写方式的内向外:

WHERE ...
  AND CASE WHEN condition1 THEN MATCH(col1) AGAINST...
           WHEN condition 2 THEN MATCH(col2) AGAINST ...
           ELSE MATCH(col3) AGAINST ... END
 AND ...

换句话说,当condition1为true时,WHERE子句应该评估与col1的匹配是否是命中等的真实性。

然后,CASE表达式应该根据该行的相应匹配结果为每一行返回true,从而为您提供预期的结果。

我强调"应该。"

这在逻辑上是正确的,不使用全文的标签,我还没有机会验证。由于全文搜索在整个表上运行,无论如何,并且要求服务器过滤掉其他谓词排除的行,因此这种查询的性能也会很有趣。

答案 1 :(得分:0)

尝试替换所有匹配()

 match (CASE WHEN store_products_description.products_title IS NOT NULL
         AND store_products_description.products_title!='' THEN 
             store_products_description.products_title
        ELSE products_description.products_title
         END AS products_title) against ('bu*' IN boolean mode)

使用

match (CASE WHEN store_products_description.products_title IS NOT NULL
         AND store_products_description.products_title!='' THEN 
             store_products_description.products_title
        ELSE products_description.products_title
         END ) against ('bu*' IN boolean mode)

match()内提供别名可能会导致问题。希望这会有所帮助