簡體   English   中英

獲取具有選項的產品

[英]Get products that has options

我有一個查詢,現在我需要獲取所有具有某些過濾器選項的產品。 使用Laravel,您可以使用whereHas僅獲取具有過濾器選項的產品。 如何使用“原始”查詢來做到這一點?

雄辯的建設者:

    $db = app( 'db' )
        ->table( 'products' )
        ->join( 'product_category', 'product_category.product_id', '=', 'products.id' )
        ->rightJoin( 'categories', 'categories.id', 'product_category.category_id' )
        ->leftJoin( 'brands', 'brands.id', '=', 'products.brand_id' )
        ->leftJoin( 'variants', 'variants.product_id', '=', 'products.id' )
        ->leftJoin( 'product_contents', 'product_contents.product_id', '=', 'products.id' )
        ->leftJoin( 'translations_languages', 'translations_languages.id', '=', 'product_contents' . '.language_id' )
        ->leftJoin( 'filter_option_product', 'filter_option_product.product_id', '=', 'products.id' )
        ->where( 'categories.id', '3396326' )
        ->where( 'variants.is_default', true )
        ->where( 'translations_languages.title_short_two', 'nl' )
        ->where( 'filter_option_product.option_id', 177 )
        ->whereIn( 'products.id', [ 31025567, 36117839, 36259742, 36260666 ] )
->get();

原始查詢:

SELECT
    *
FROM
    `products`
INNER JOIN `product_category` ON `product_category`.`product_id` = `products`.`id`
RIGHT JOIN `categories` ON `categories`.`id` = `product_category`.`category_id`
LEFT JOIN `brands` ON `brands`.`id` = `products`.`brand_id`
LEFT JOIN `variants` ON `variants`.`product_id` = `products`.`id`
LEFT JOIN `product_contents` ON `product_contents`.`product_id` = `products`.`id`
LEFT JOIN `translations_languages` ON `translations_languages`.`id` = `product_contents`.`language_id`
LEFT JOIN `filter_option_product` ON `filter_option_product`.`product_id` = `products`.`id`
WHERE
    `categories`.`id` = 3396326
AND `variants`.`is_default` = true
AND `translations_languages`.`title_short_two` = 'nl'
AND `products`.`id` IN(31025567, 36117839, 36259742, 36260666)
AND (`filter_option_product`.`option_id` = 174 AND `filter_option_product`.`option_id` = 1)

如果要在filter_option_product確實有任何行的行 ,則在where子句中測試該表中的列是否不為空:

SELECT
    *
FROM
    `products`
INNER JOIN `product_category` ON `product_category`.`product_id` = `products`.`id`
RIGHT JOIN `categories` ON `categories`.`id` = `product_category`.`category_id`
LEFT JOIN `brands` ON `brands`.`id` = `products`.`brand_id`
LEFT JOIN `variants` ON `variants`.`product_id` = `products`.`id`
LEFT JOIN `product_contents` ON `product_contents`.`product_id` = `products`.`id`
LEFT JOIN `translations_languages` ON `translations_languages`.`id` = `product_contents`.`language_id`
LEFT JOIN `filter_option_product` ON `filter_option_product`.`product_id` = `products`.`id`
WHERE NOT(`filter_option_product`.`option_id` IS NULL)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM