【问题标题】:Dynamic alias of aggregate functions on laravel's query builderlaravel 查询生成器中聚合函数的动态别名
【发布时间】:2021-06-12 07:54:26
【问题描述】:

我想要一个 count() 函数的动态别名。

我使用 mysql 5.7 作为数据库。

这是代码。

$date = Carbon::parse('2021-03-11')
DB::table('payments')
        ->select(DB::raw('IFNULL(sum(amount),0) as :date'), $date)
        ->get();

而且我有这个错误

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, `2021-03-11` from `payments`' at line 1 (SQL: select IFNULL(sum(amount),0) as :date, `2021-03-11` from `payments`)

我期待这个结果

|2021-03-11|
|<amount_here>|

【问题讨论】:

    标签: mysql laravel laravel-query-builder


    【解决方案1】:

    试试这样的:

    $date = Carbon::parse('2021-03-11')
    DB::table('payments')
            ->select(DB::raw('IFNULL(sum(amount),0) as "?"', [$date])
            ->get();
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-05
      • 2014-12-15
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 2019-01-28
      相关资源
      最近更新 更多