gpt4 book ai didi

php - 获取有选项的产品

转载 作者:行者123 更新时间:2023-11-29 18:38:19 24 4
gpt4 key购买 nike

我有一个查询,现在我需要获取具有某些过滤选项的所有产品。使用 Laravel,您可以使用 whereHas 来仅获取具有过滤器选项的产品。我如何使用“原始”查询来做到这一点?

Eloquent build 者:

    $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 子句中测试该表中的列是否为 null:

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)

关于php - 获取有选项的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108088/

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