使用2个数据库连接进行Laravel查询

时间:2017-07-10 17:48:55

标签: php mysql database laravel eloquent

我有那个可以在phpmyadmin中完美执行的查询:

SELECT f.`id`, f.`cod_factura`,f.`empresa` FROM 
`gen`.`facturas` f WHERE f.`id_cliente` IN(SELECT 
`id` FROM `gen`.`clientes` WHERE `dni` IN(SELECT 
`dni` FROM `ciiis17`.`users` WHERE `id` IN(SELECT `user_id` FROM 
`ciiis17`.`registrations` WHERE `has_bill`=1))) AND `enviado`=0  
AND `concepto`='1' 

现在我尝试在Laravel中进行查询。我正在尝试这样的事情:

$result = DB::connection('gen')
              ->table('facturas')
              ->select(DB::raw('id, cod_factura, empresa'))
              ->whereIn('id_cliente', DB::table('clientes')
                                      ->select(DB::raw('id'))
                                      ->whereIn('dni', DB::connection('ciiis17')
                                                       ->table('users')
                                                       ->select(DB::raw('dni'))
                                                       ->whereIn('id', DB::table('registrations')
                                                                       ->select(DB::raw('user_id'))
                                                                       ->where(['has_bill', 1],['enviado', 0],['concepto', '1']))))->get();

但是我收到了这个错误:

“Connection.php第729行中的QueryException: SQLSTATE [42S02]:未找到基表或视图:1146表'gen.registrations'不存在“

它要求“gen.registrations”但它应该是“ciiis17.registrations”

最终执行的SQL是:

"select id, cod_factura, empresa from `facturas` where `id_cliente` in 
(select id from `clientes` where `dni` in (select dni from `users` where 
`id` in (select user_id from `registrations` where (`0` = ? and `1` = ?))))"

由于

0 个答案:

没有答案