我是 Laravel 新手,但我想创建此查询(MySQL):
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 '%%')
查看文档我找不到正确的方法。谢谢。
最佳答案
通常,对于 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
列语句放入其中。
关于php - 在 Laravel 4.2 中创建 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46409173/