【问题标题】:how to find comma separated values from mysql table如何从mysql表中查找逗号分隔值
【发布时间】:2021-02-14 20:22:00
【问题描述】:

我有两张桌子。我加入了两个表。

在控制器中:

$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();
        }

在上面的查询中,在循环文件表中我有一列 unique_id_for_group。我必须在 unique_id_for_group

的帮助下获取行

我看到了这种类型的数组:

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
                        )

                )

        )

)

我需要这样的输出:

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

表示区域应该用逗号分隔值和标题相同,所以标题应该是唯一的。

我试过了:

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

我不知道该怎么做?

【问题讨论】:

  • 显示您的查询,您正在为此集合使用哪个查询?
  • 请检查我的问题。我更新了这个。

标签: php arrays laravel eloquent


【解决方案1】:

我得到了答案:

$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();

【讨论】:

    【解决方案2】:

    试试下面的代码:

    $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();
    

    【讨论】:

      猜你喜欢
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多