比如:

select id, name, count(post) from ...

在laravel中:

$user = $this
    ->select(
            'id',
            'name',
            DB::raw('count(post)')
    )
     ->join(...)
     ->first()

执行后会报错: laravel Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause 

但是后面显示的sql语句放到mysql中可以执行,原因是laravel默认使用严格模式,在config/database.php中关闭

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false, // 关闭严格模式
            'engine' => null,
        ],

相关文章:

  • 2021-12-07
  • 2021-05-15
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
  • 2022-02-22
  • 2022-12-23
  • 2021-10-25
猜你喜欢
  • 2022-12-23
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案