w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
MySQL join multiple tables and limit the output from "ON"

You can do it by having an inline query. I am not sure how this will perform when you have more data.

    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




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.