2017-09-25 92 views
0

创建SQL查询我是新来Laravel但我想创建这个查询(MySQL的):在Laravel 4.2

SELECT 
    * 
FROM 
    (
     SELECT 
      ce.empresa_id, 
      CONCAT(ce.nombre, ' ', ce.apellido) AS nombre, 
      ce.grupo_contable 
     FROM 
      cliente_empresa AS ce 
     UNION 
      SELECT 
       cc.empresa_id, 
       cc.nombre, 
       cc.grupo_contable 
      FROM 
       cuenta_contable AS cc 
      UNION 
       SELECT 
        cci.empresa_id, 
        cci.grupo_iva AS nombre, 
        cci.cuenta_contable AS grupo_contable 
       FROM 
        cuenta_contables_iva AS cci 
    ) AS cuentasContables 
WHERE 
    cuentasContables.empresa_id = 1 
AND (cuentasContables.nombre LIKE '%a%' 
OR cuentasContables.grupo_contable LIKE '%%') 

看文档,我不能找到合适的方式来做到这一点。谢谢。

+0

在这里,你去包含你需要的一切https://laravel.com/docs/4.2/queries – Maantje

+0

@Maantje http://prntscr.com/gpixdv –

+1

你已经尝试过什么(任何代码?),我愿意帮忙只是想从你身边看到一些努力。 – Maantje

回答

0

通常情况下,该ORM无法管理的查询时,你可能会想简单地调用\DB::raw

$results = \DB::table('users') 
    ->select(\DB::raw(/* your stuff here */)) 
你的情况

然而,你可能要考虑粘到ORM。看起来你可能想尝试这样的:

$first = DB::table('cliente_empresa') 
     ->where('empresa_id', '=', 1); 

$second = DB::table('cuenta_contable') 
     ->where('empresa_id', '=', 1); 

$results = DB::table('cuenta_contables_iva') 
     ->where('empresa_id', '=', 1) 
     ->union($first) 
     ->union($second) 
     ->get(); 

很显然,你需要把你的SELECT列声明中有作为。