【问题标题】:Create SQL Query in Laravel 4.2在 Laravel 4.2 中创建 SQL 查询
【发布时间】:2017-09-25 15:44:00
【问题描述】:

我是 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 '%%')

查看文档我找不到正确的方法。谢谢。

【问题讨论】:

标签: php mysql laravel orm eloquent


【解决方案1】:

通常,对于 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 列语句也放在那里。

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多