gpt4 book ai didi

mysql - SQL JOIN 查询仅在一张表上使用 GROUP BY 获取数据

转载 作者:行者123 更新时间:2023-11-29 17:35:50 25 4
gpt4 key购买 nike

我正在尝试从多个表中获取数据,其中我有一个产品,其详细信息分布在多个表中。这是我正在使用的一个查询:

SELECT 
fp.id, fp.name, fp.slug, fp.status, fp.approved, fp.created_at, fp.updated_at,
fc.name as cat_name, fc.slug as category_slug,
ff.filepath,
fdp.type, fdp.sku, fdp.regular_price, fdp.sale_price

FROM `foduu_products` fp

JOIN `foduu_category_product` fcp
ON fp.id = fcp.product_id

JOIN `foduu_filemanager` ff
on fp.filemanager_id = ff.id

JOIN `foduu_categories` fc
on fcp.category_id = fc.id

JOIN `foduu_details_product` fdp
ON fp.id= fdp.product_id

where fcp.category_id in (39,155,30,41,51)

group by fdp.product_id <<<<-------------------- Here is the problem

ORDER by fp.id ASC

当我使用GROUP BYfoduu_details_product获取数据时,它保存一个product_id的多个记录。当我运行此查询时,我收到此错误。

#1055 - Expression #8 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'foduu-ecommerce-cms.fc.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

不明白为什么 GROUP BY 总是给我这些简短的错误。有什么建议可以改进我的查询吗?

这是我的数据库表SQL供引用:

http://vbought.com/products%20table.sql

谢谢(提前)!

最佳答案

您的查询中没有任何聚合函数,因此您应该首先质疑此特定语句的使用。

您处于“sql_mode=only_full_group_by”状态,因此您必须重复 select 中未在 group by 语句中的聚合函数(Max、Sum、Count...)中使用的每一列。

关于mysql - SQL JOIN 查询仅在一张表上使用 GROUP BY 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50287743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com