MySQL连接多个表并限制从;关于;

MySQL连接多个表并限制从;关于;,mysql,join,inner-join,Mysql,Join,Inner Join,当我编写MySQL查询时,出现了一个问题。这是我的问题 SELECT SUM(view_product_count_details.view_product_count) AS count_sum, product_details.product_name, product_details.product_url, product_details.product_price, product_image_details.product_image_nam

当我编写MySQL查询时,出现了一个问题。这是我的问题

SELECT 
    SUM(view_product_count_details.view_product_count) AS count_sum,
    product_details.product_name,
    product_details.product_url,
    product_details.product_price,
    product_image_details.product_image_name,
    main_category_details.main_category_url,
    sub_category_details.sub_category_url
FROM
    view_product_count_details
        JOIN
    product_details ON view_product_count_details.product_id_fk = product_details.product_id
        JOIN
    product_image_details ON product_image_details.product_id_fk = view_product_count_details.product_id_fk
        JOIN
    main_category_details ON product_details.product_main_cat_id = main_category_details.main_category_id
        JOIN
    sub_category_details ON product_details.product_sub_cat_id_fk = sub_category_details.sub_category_id
WHERE
    view_product_count_details.view_product_status = 'active'
GROUP BY view_product_count_details.product_id_fk
ORDER BY count_sum DESC
LIMIT 4
这里我有一个产品的多个图像。这些图像在表“product\u image\u details”中。此查询返回count作为图像数,其中我需要存储在表“view\u product\u count\u details”中的人员查看的产品数。当我选择计数时,我得到了计数。但当我加入“product_image details”表时,结果就错了。有没有办法在单个查询中完成


请帮帮我。。。提前感谢……)

您可以通过使用内联查询来实现。我不确定当您有更多数据时,这将如何执行

    SELECT table1.*,product_image_details.product_image_name FROM
    (
        SELECT 
            SUM(view_product_count_details.view_product_count) AS count_sum,
            product_details.product_id,
            product_details.product_name,
            product_details.product_url,
            product_details.product_price,
            main_category_details.main_category_url,
            sub_category_details.sub_category_url
        FROM
            view_product_count_details
                JOIN
            product_details ON view_product_count_details.product_id_fk = product_details.product_id
                JOIN
            product_image_details ON product_image_details.product_id_fk = view_product_count_details.product_id_fk
                JOIN
            main_category_details ON product_details.product_main_cat_id = main_category_details.main_category_id
                JOIN
            sub_category_details ON product_details.product_sub_cat_id_fk = sub_category_details.sub_category_id
        WHERE
            view_product_count_details.view_product_status = 'active'
        GROUP BY view_product_count_details.product_id_fk
        ORDER BY count_sum DESC
        LIMIT 4
    ) table1
        JOIN
    product_image_details ON product_image_details.product_id_fk = table1.product_id    
    LIMIT 4

你能发布表模式的视图吗?产品数量有多少?或者您需要计算表中查看\产品\计数\详细信息的行数并获取计数吗?问题可能是该产品的每个产品\图像\详细信息都会计算一次查看\产品\计数。当我尝试发布表架构时,它会显示大小错误。我是新来的,所以不能把它也贴出来作为答案(我认为问题正是“Kickstart”所说的。当我在没有图像的情况下获取数据时,它没有造成问题。谢谢你的快速回复。但它不起作用:(.它显示了错误“[Err]1054-未知列‘查看产品数量’详细信息。产品id’fk’在‘on子句’”中的“查看产品数量”拥有每个产品的计数。当我进行此查询时,计数会根据图像的数量递增。也就是说,如果有计数1和3个图像,它会将计数显示为3。您可以为数据库提供架构图吗?这将有助于更好地理解。您需要更改子查询以返回视图\产品\计数\详细信息。p然后在子查询和产品图像详细信息之间的联接中引用它(使用返回的名称,可能是table1.product\u id\u fk)。@Kickstart谢谢:)我根据您的评论进行了更新。