简体   繁体   English

如何基于相同的列值合并表行数据

[英]How to merge table rows data based on same column value

I have table name abc and it's columns are 我有表名称abc,它的列是

id    table_row_name   a     b    c
1     user             1     1    Null
2     user             Null  Null 1  

How can I merge into one record with query like below and what will be the laravel way of doing it 我如何通过如下所示的查询合并到一条记录中,如何使用laravel进行合并?

      table_row_name   a     b    c
      user             1     1    1

You can try below query 您可以在下面查询

select table_row_name, max(a) as a, max(b) as b, max(c) as c
from tablename
group by table_row_name

You can simply achieve that by this query: 您可以通过以下查询简单地实现这一点:

\DB::table('abc')
        ->select([
            'table_row_name',
            \DB::raw('MAX(a) as a'),
            \DB::raw('MAX(b) as b'),
            \DB::raw('MAX(c) as c')
        ])
        ->groupBy('table_row_name')
        ->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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