Union queries from different databases in Laravel Query Builder

后端 未结 2 1420
感动是毒
感动是毒 2021-01-03 14:33

I have two similar tables in two different databases. Both tables have a column with a date and one with email addresses. Though the column names are not the same. As resul

2条回答
  •  执念已碎
    2021-01-03 15:22

    You can't use different connections, but you still can do it providing the db name explicitly:

    $q1 = DB::table('db1.contacts')
           // where(..) or anything you need here
           ->select('mail_address as email', 'date as created_at');
    
    $q2 = DB::table('db2.contacts')
           // like above
           ->select('email', 'created_at');
    
    $result = $q2->union($q1)->get();
    

提交回复
热议问题