A.* isn't in GROUP BY with left join on laravel query builder

前端 未结 4 521
生来不讨喜
生来不讨喜 2020-12-12 01:14
   $search_alls=
    DB::table(\'a16s as A\')
    ->select(\'A.id\')
    // ->select(\'A.*\')
    ->addSelect(DB::raw(\'SUM(CASE WHEN B.approve = 1 ELSE 0 E         


        
相关标签:
4条回答
  • 2020-12-12 01:27

    You have to turn off sctrict mode in Laravel application.

    go to config/database.php

    under the MySQL set

    strict => false
    

    and it will start working.

    Hope this helps.

    0 讨论(0)
  • 2020-12-12 01:30

    I saw your issue in detail. and I've faced same issue. in database.php, there are below setting

    'mysql' => [
                'driver' => 'mysql',
                'url' => env('DATABASE_URL'),
                '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', ''),
                'unix_socket' => env('DB_SOCKET', ''),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'prefix_indexes' => true,
                'strict' => false,
                'engine' => null,
                'options' => extension_loaded('pdo_mysql') ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                ]) : [],
            ],
    

    When I set strict property as false, it was working, and i've solved issue.

    0 讨论(0)
  • 2020-12-12 01:34

    To fix this issue you need to specify required columns in select list and group by clause

    $search_alls=DB::table('a16s as A')
                    ->select('A.id','A.name')
                    ->addSelect(DB::raw('SUM(CASE WHEN B.approve = 1 ELSE 0 END) as Yshow'))
                    ->leftjoin('a16s_likes as B', function($join) {
                        $join->on('A.id', '=', 'B.p_id');
                    })
                    ->groupBy('A.id')
                    ->groupBy('A.name');
        ->get();
    

    As per newer release mysql 5.7 does not permit queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are not named in the GROUP BY clause

    12.19.3 MySQL Handling of GROUP BY


    As per docs

    MySQL 5.7.5 and up implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them. (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default

    0 讨论(0)
  • 2020-12-12 01:44

    use select('A.*') to select all columns

     $search_alls=
        DB::table('a16s as A') 
        ->select('A.*')
        ->addSelect(DB::raw('SUM(CASE WHEN B.approve = 1 ELSE 0 END) as Yshow'))
        ->leftjoin('a16s_likes as B', function($join) {
            $join->on('A.id', '=', 'B.p_id');
            })
        ->groupBy('A.id')
        ->get();
    
    0 讨论(0)
提交回复
热议问题