简体   繁体   中英

Select all columns but distinct from database table in Laravel

Bellow SQL command runs perfectly :

select * from `product` group by `owner_name` order by `id` asc 

When I translate above code in my Laravel project to get the same result :

Product::select('*')
        ->orderBy('id','asc')->groupBy('owner_name')
         ->get();

This laravel code returns me error that

SQLSTATE[42000]: Syntax error or access violation: 1055 'db1.product.id' isn't in GROUP BY (SQL: select * from product group by owner_name order by id asc)

Problem is I have many duplicated records with slight differences on some of their columns. I need to get them by owner_name and only one time .

Edit your applications's database config file config/database.php

In mysql array, set strict => false to disable MySQL's strict mode

You have don't need to do 'select(*)', by default it will select all columns data.

Try this:

Product::orderBy('id','asc')->groupBy('owner_name')
     ->get();

And if you want to fetch selected column you can do like this:

Product::select(['column_1', 'column_2'])
    ->orderBy('id','asc')->groupBy('owner_name')
     ->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM