简体   繁体   English

如何从mysql表中找到逗号分隔的值

[英]how to find comma separated values from mysql table

I have two tables.我有两张桌子。 I joined both table.我加入了两张桌子。

In controller:在控制器中:

$data = array();
$distinct_unique_id_for_group = circulateFile::select('unique_id_for_group')->distinct()->get();
  foreach($distinct_unique_id_for_group as $distinct)
  {  
   $data[] = DB::table('circulate_files')
                    ->join('regionmasters','circulate_files.region_id','=','regionmasters.id')
                    ->where('circulate_files.unique_id_for_group','=',$distinct->unique_id_for_group)
                    ->select('circulate_files.*','regionmasters.region')
                    ->get();
        }

In above query, In circulateFiles table I have a column unique_id_for_group .在上面的查询中,在circleFiles表中,我有一列unique_id_for_group I have to get rows with the help of unique_id_for_group我必须在unique_id_for_group的帮助下获取行

and I got this type of array in view:我看到了这种类型的数组:

Array
(
    [0] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 2
                            [title] => title1
                            [region] => east
                        )

                    [1] => stdClass Object
                        (
                            [id] => 3
                            [title] => title1
                            [region] => west
                        )

                )

        )

    [1] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 4
                            [title] => title2
                            [region] => east
                        )

                    [1] => stdClass Object
                        (
                            [id] => 5
                            [title] => title2
                            [region] => south
                        )

                )

        )

)

I need output like this:我需要这样的输出:

  REGION           | TITLE
 
   east, west      | title1  
   east, south     | title2

Means region should be in comma separated value and title is same, so title should be unique.意味着区域应该是逗号分隔的值并且标题是相同的,所以标题应该是唯一的。

I tried by this:我试过这个:

 @foreach($data as $arraydata)
   <tr>
    <td>{{ $arraydata->region }}</td>
    </tr>
 @endforeach 

I am not getting idea how to do that?我不知道该怎么做?

I got my answer:我得到了我的答案:

$data = DB::table('circulate_files')
                    ->join('regionmasters','circulate_files.region_id','=','regionmasters.id')
                    ->select('circulate_files.title','circulate_files.file','circulate_files.created_at','unique_id_for_group', DB::raw('group_concat(region) as new_region'))
                    ->groupBy('unique_id_for_group')
                    ->get();

Try the Following code:尝试以下代码:

$data = DB::table('circulate_files')
       ->selectRaw('circulate_files.title, unique_id_for_group, 
            group_concat(region) as new_region
        ')
        ->join('regionmasters', 'circulate_files.region_id', 'regionmasters.id')
        ->groupBy('unique_id_for_group')
        ->get();

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

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