【问题标题】:SQL query fails, no idea whySQL查询失败,不知道为什么
【发布时间】:2018-05-02 12:47:58
【问题描述】:

所以我刚刚升级到 MySQL 8 来测试它是否是一个可行的选择,但我偶然发现了这个谜题。

它在“table_product_features AS pf”处引发错误,但我似乎无法弄清楚为什么在文档中它以完全相同的方式完成:https://dev.mysql.com/doc/refman/8.0/en/join.html

错误:

您的 SQL 语法有错误;查看与您的 MySQL 服务器版本相对应的手册,以在第 1 行 (1064) 的“groups ON pf.parent_id = groups.feature_id LEFT JOIN table_product_features_des”附近使用正确的语法

我还检查了错误跟踪器,但这似乎与我的问题没有任何关系。

我到底做错了什么?

附:我把它放在一个代码 sn-p 中,这样它就会保持格式化。

SELECT pf.feature_id, 
       pf.company_id, 
       pf.feature_type, 
       pf.parent_id, 
       pf.display_on_product, 
       pf.display_on_catalog, 
       pf.display_on_header, 
       table_product_features_descriptions.description, 
       table_product_features_descriptions.lang_code, 
       table_product_features_descriptions.prefix, 
       table_product_features_descriptions.suffix, 
       pf.categories_path, 
       table_product_features_descriptions.full_description, 
       pf.status, 
       pf.comparison, 
       pf.position, 
       groups.position AS group_position, 
       table_product_features_values.value, 
       table_product_features_values.variant_id, 
       table_product_features_values.value_int 
FROM   table_product_features AS pf 
       LEFT JOIN table_product_features AS groups 
              ON pf.parent_id = groups.feature_id 
       LEFT JOIN table_product_features_descriptions 
              ON table_product_features_descriptions.feature_id = pf.feature_id 
                 AND table_product_features_descriptions.lang_code = 'en' 
       INNER JOIN table_product_features_values 
               ON table_product_features_values.feature_id = pf.feature_id 
                  AND table_product_features_values.product_id = 230 
                  AND table_product_features_values.lang_code = 'en' 
       INNER JOIN table_ult_objects_sharing 
               ON ( table_ult_objects_sharing.share_object_id = pf.feature_id 
                    AND table_ult_objects_sharing.share_company_id = 1 
                    AND table_ult_objects_sharing.share_object_type = 
                        'product_features' ) 
WHERE  1 
       AND pf.feature_type != 'G' 
       AND pf.status IN ( 'A' ) 
       AND pf.display_on_product = 'Y' 
       AND ( pf.categories_path = '' 
              OR Isnull(pf.categories_path) 
              OR Find_in_set(203, pf.categories_path) 
              OR Find_in_set(215, pf.categories_path) 
              OR Find_in_set(216, pf.categories_path) ) 
GROUP  BY pf.feature_id 
ORDER  BY group_position, 
          pf.position, 
          table_product_features_descriptions.description 

【问题讨论】:

  • 简化问题,看看stackoverflow.com/help/mcve
  • 您得到的确切错误是什么?
  • 你的group by 不应该工作。将所有未聚合的列放在group by 子句中。
  • @Matt 我在描述中添加了错误。

标签: sql mysql-8.0


【解决方案1】:

如果您不使用聚合函数,我不明白您为什么需要 GROUP BY

Also groups is a reserved word,把它改成tgroups或者别的什么。

SELECT pf.feature_id, 
       pf.company_id, 
       pf.feature_type, 
       pf.parent_id, 
       pf.display_on_product, 
       pf.display_on_catalog, 
       pf.display_on_header, 
       tdesc.description, 
       tdesc.lang_code, 
       tdesc.prefix, 
       tdesc.suffix, 
       pf.categories_path, 
       tdesc.full_description, 
       pf.status, 
       pf.comparison, 
       pf.position, 
       tgroups.position group_position, 
       tvals.value, 
       tvals.variant_id, 
       tvals.value_int 
FROM   table_product_features pf 
       LEFT JOIN table_product_features tgroups ON pf.parent_id = tgroups.feature_id 
       LEFT JOIN table_product_features_descriptions tdesc ON tdesc.feature_id = pf.feature_id AND tdesc.lang_code = 'en' 
       INNER JOIN table_product_features_values tvals ON tvals.feature_id = pf.feature_id AND tvals.product_id = 230 AND tvals.lang_code = 'en' 
       INNER JOIN table_ult_objects_sharing tshar ON (tshar.share_object_id = pf.feature_id AND tshar.share_company_id = 1 AND tshar.share_object_type = 'product_features') 
WHERE  1 
AND pf.feature_type != 'G' 
AND pf.status IN ('A') 
AND pf.display_on_product = 'Y' 
AND (pf.categories_path = '' OR Isnull(pf.categories_path) OR Find_in_set(203, pf.categories_path) OR Find_in_set(215, pf.categories_path) OR Find_in_set(216, pf.categories_path)) 
ORDER BY group_position, pf.position, tdesc.description 

【讨论】:

  • 它是一个后备,查询是动态生成的,可以通过钩子来操作。在这种情况下,它确实可以被删除。
猜你喜欢
  • 2017-04-24
  • 2014-04-08
  • 2020-01-04
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 2012-04-01
  • 2011-05-24
相关资源
最近更新 更多