2016-12-02 2 views
2

私は2つのテーブル、組織、連絡先を持っています。どちらの表にも「電子メール」という列がありますが、組織の名前はそのままにしておく必要がありますが、電子メールの列にはすべての電子メール(組織の+すべての連絡先電子メール)を連結します。Laravelはテーブルを結合して行を連結します

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

2)この1つは実際には非常に近いですが、それはフィルタで除外されていません。ここで

は、私は運

1で試したいくつかのバージョンでは)この1つはグループではないんですどちらも接触または顧客のentiresが、私は(私はそれだけでなしの電子メールと連絡先を除外します)知っているサブクエリに近づくことを試みた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(); 

3)持つエントリいくつかの例と言う)

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

1/2 PDOException in Connection.php line 333: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? group by customers . id ' at line 1

2/2 QueryException in Connection.php line 713: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? group by customers . id ' at line 1 (SQL: select customers . id , customers . name , registration , GROUP_CONCAT(DISTINCT contacts.email, ", ") AS contact_emails, customers . email from customers left join contacts on contacts . customer_id = customers . id and LENGTH(contacts.email) 4 group by customers . id)

3.3:この1つは、以下の構文エラーを生成する)

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

3.2存在しないJoinClause::whereRaw():エラーのサブクエリ1件の結果を試す

3.1)私はこのようにしなければならないが、これはエラーをもたらす。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')); 
    }) 
+0

私の頭に浮かぶのはサブクエリです:http://dev.mysql.com/doc/refman/5.7/en/subqueries.html - しかし、実際問題はあなたがすべての電子メールを連結したいのです。後でスクリプトの中で別々の形で価値があるのではないでしょうか? – Kjell

+0

私は、yajraのデータテーブルを使用して1つのテーブルに結果を供給しています。 –

+0

@Kjell私はサブクエリを実際に試しました。失敗した例で私の質問を更新しました。 –

答えて

1

これは私のために働く。構文エラーず、4文字未満の長さとの接触除外しませ:Laravel 5.3.26、MySQLの5.6.20(なしstrictモード)でテスト

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

を。

関連する問題