Laravel 连接表并连接行

Edm*_*nok 4 mysql laravel laravel-5

所以我有两个表,组织和联系人。两个表都有“电子邮件”列,我需要做的是保留组织的名称,但在电子邮件列中连接所有电子邮件(组织+所有联系人电子邮件)。

这是我尝试过的一些版本,但没有成功

1)这个不分组:

$customers = DB::table('customers')
    ->whereRaw('LENGTH(customers.email) > 4')
    ->select([
        'customers.id',
        'customers.name',
        'customers.email'
    ]);

$contacts = DB::table('contacts')
    ->whereRaw('LENGTH(contacts.email) > 4')
    ->leftJoin('customers', 'contacts.customer_id', '=', 'customers.id')
    ->select([
        'customers.id',
        'customers.name',
        'contacts.email'
    ]);

return $customers
    ->union($contacts)
    ->select([
        'id',
        'name',
        DB::raw('GROUP_CONCAT(DISTINCT email, ", ") AS emails'),
    ])
    ->groupBy('id')
    ->get();
Run Code Online (Sandbox Code Playgroud)

2)这个实际上非常接近,但它不会过滤掉联系人或客户整体都没有的条目DB::raw('LENGTH(email) > 4')

return $customers = DB::table('customers')
                ->leftJoin('contacts', 'contacts.customer_id', '=', 'customers.id')
                ->select([
                    'customers.id',
                    'customers.name',
                    'registration',
                    DB::raw('GROUP_CONCAT(DISTINCT contacts.email, ", ") AS contact_emails'),
                    'customers.email'
                ])
                ->groupBy('customers.id')
                ->get();
Run Code Online (Sandbox Code Playgroud)

3)我尝试更接近子查询(我知道它只会过滤掉没有电子邮件的联系人)

3.1) 尝试子查询 1 会导致错误:JoinClause::whereRaw()不存在

return $customers = DB::table('customers')
            ->leftJoin('contacts', function($join) {
                $join->on('contacts.customer_id', '=', 'customers.id')
                    ->whereRaw('LENGTH(email) > 4');
            })...
Run Code Online (Sandbox Code Playgroud)

3.2) 这会产生以下语法错误:

return $customers = DB::table('customers')
            ->leftJoin('contacts', function($join) {
                $join->on('contacts.customer_id', '=', 'customers.id')
                    ->where(DB::raw('LENGTH(email) > 4'));
            })
Run Code Online (Sandbox Code Playgroud)

Connection.php 第 333 行中的1/2 PDOException:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 '? 附近使用的正确语法。通过...分组customersid' 在第 1 行

Connection.php 第 713 行中的2/2 QueryException:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 '? 附近使用的正确语法。通过...分组customersid' 在第 1 行(SQL: select customers. id, customers. name, registration, GROUP_CONCAT(DISTINCT contacts.email, ", ") AS contact_emails, customers. emailfrom customersleft join contactson contacts. customer_id= customers. idand LENGTH(contacts.email) 4 group by customers. id

3.3)一些例子说我应该这样做,但这会产生错误Not enough arguments for the on clause.

return $customers = DB::table('customers')
    ->leftJoin('contacts', function($join) {
        $join->on('contacts.customer_id', '=', 'customers.id');
        $join->on(DB::raw('LENGTH(contacts.email) > 4'));
    })
Run Code Online (Sandbox Code Playgroud)

Tom*_*ler 5

这对我有用。没有语法错误并过滤掉长度小于 4 个字符的联系人:

DB::table('customers')
  ->leftJoin('contacts', function ($join) {
      $join->on('contacts.customer_id', '=', 'customers.id')
              ->where(DB::raw('length(contacts.email)'), '>', 4);
  })
  ->select([
      'customers.id',
      'customers.name',
      DB::raw('group_concat(distinct contacts.email separator ", ") AS contact_emails'),
  ])
  ->groupBy('customers.id')
  ->get();
Run Code Online (Sandbox Code Playgroud)

在 Laravel 5.3.26、MySQL 5.6.20 中测试(无严格模式)。