gpt4 book ai didi

mysql - 如何使用 Laravel 的流畅查询构建器选择计数和使用位置?

转载 作者:可可西里 更新时间:2023-11-01 08:03:33 25 4
gpt4 key购买 nike

以我为例

$products = $products->select(array('products.*', \DB::raw('COUNT(extra_fields.price) as price_count')))->leftJoin('extra_fields', 'products.id', '=', 'extra_fields.item_id')
->where('item_type', 'App\Models\Product')
->whereRaw("price = '0'")
->whereRaw("price_count = 1")
->groupBy('item_id')
->groupBy('price')

;

当我想在 where 或 whereRaw 中使用 {price_count} 时:

Column not found: 1054 Unknown column 'price_count'

price_count 只能以这种方式工作:

->orderBy('price_count')

我如何在 where 或 whereRaw 上使用 price_count?

有什么建议吗?

最佳答案

您可以使用 havingRaw作为:

->havingRaw('price_count = 1')

->having('price_count', '=', 1)

The havingRaw method may be used to set a raw string as the value of the having clause.

->havingRaw('COUNT(extra_fields.price) = 1')

关于mysql - 如何使用 Laravel 的流畅查询构建器选择计数和使用位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41330886/

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