2017-09-25 96 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列聲明中有作爲。