【问题标题】:Laravel Query Builder - Ordering on a Custom ColumnLaravel 查询生成器 - 在自定义列上排序
【发布时间】:2015-11-05 08:34:33
【问题描述】:

我想对我的 Laravel 查询构建器结果在一个自定义列(名字和姓氏的连接)上进行排序。

我所做的是-

    $summary = DB::table('service_rating')
                ->join('partners', 'partners.id',   '=', 'service_rating.partner_id')
                ->join('users',     'users.id',     '=', 'partners.user_id')
                ->select(
                            DB::raw("CONCAT( users.first_name,' ', users.last_name) as lawn_pro"),
                            DB::raw ('AVG(service_rating.rating)                    as rating'),
                            DB::raw ('COUNT(service_rating.rating)                  as jobs'),
                            DB::raw ('SUM(service_rating.rating)                    as payout')
                        )
                ->where('customer_id', '=', Auth::user()->id)
                ->whereRaw('service_rating.created_at >= DATE(NOW()) - INTERVAL '.$no_of_day_to_show.' DAY')
                ->groupBy('service_rating.partner_id')
                ->orderBy('lawn_pro', 'asc');

所以,我收到这条线的错误 -

                ->orderBy('lawn_pro', 'asc');

而且错误是这样的-

有人可以帮忙吗?

【问题讨论】:

  • ->orderBy("CONCAT( users.first_name,' ', users.last_name)","asc") ?

标签: php mysql laravel laravel-5 query-builder


【解决方案1】:

显然您在查询中使用了count() 函数,这会忽略选择属性,因为我们只想知道行数。因此,lawn_pro 在查询中不可用。

我建议执行查询,然后计算可用行数。

$rows = $summary->get();
$count = count($rows);

【讨论】:

  • 如果我需要计数,我不会使用orderby
  • 那为什么你的查询以SELECT count(*) FROM ...开头。请向我们展示此查询的所有完整代码,以便我们了解发生了什么。
猜你喜欢
  • 2016-04-27
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 2016-11-07
  • 1970-01-01
  • 2015-12-13
相关资源
最近更新 更多