【问题标题】:How to select count and use where with Laravel's fluent query builder?如何使用 Laravel 的流畅查询构建器选择计数和使用位置?
【发布时间】:2016-12-26 11:53:52
【问题描述】:

就我而言

$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 ?

有什么建议吗?

【问题讨论】:

    标签: mysql laravel-5 eloquent


    【解决方案1】:

    您可以将havingRaw 用作:

    ->havingRaw('price_count = 1')
    

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

    havingRaw 方法可用于将原始字符串设置为having 子句的值。

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

    【讨论】:

      【解决方案2】:

      你不能随心所欲地使用汇总函数值,而不是你可以在“拥有”中使用的“哪里”。

      希望这会对你有所帮助。

      【讨论】:

        【解决方案3】:

        你不能随心所欲地使用汇总函数值,而不是你可以在“拥有”中使用的“哪里”。

        您可以将havingRaw 用作:

        ->havingRaw('price_count = 1')
        

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

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

        【讨论】:

          猜你喜欢
          • 2012-10-24
          • 1970-01-01
          • 2012-12-08
          • 2018-07-17
          • 2016-09-16
          • 2017-10-05
          • 1970-01-01
          • 2021-06-05
          • 2017-03-27
          相关资源
          最近更新 更多