[英]Select all columns but distinct from database table in Laravel
Bellow SQL command runs perfectly : 贝娄SQL命令运行完美:
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 : 当我在Laravel项目中翻译以上代码以获得相同的结果时:
Product::select('*')
->orderBy('id','asc')->groupBy('owner_name')
->get();
This laravel code returns me error that 这个laravel代码给我返回错误
SQLSTATE[42000]: Syntax error or access violation: 1055 'db1.product.id' isn't in GROUP BY (SQL: select * from
product
group byowner_name
order byid
asc)SQLSTATE [42000]:语法错误或访问冲突:1055'db1.product.id'不在GROUP BY中(SQL:从
product
组中选择*按owner_name
顺序按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 .
我只需要获得一次owner_name即可。
Edit your applications's database config file config/database.php
编辑应用程序的数据库配置文件
config/database.php
In mysql array, set strict => false
to disable MySQL's strict mode 在mysql数组中,设置
strict => false
以禁用MySQL的严格模式
You have don't need to do 'select(*)', by default it will select all columns data. 您无需执行“ select(*)”,默认情况下它将选择所有列数据。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.