Laravel 5.3复杂数据库更新查询,包括Select,Joins和CONCAT notworking

时间:2018-02-13 14:50:00

标签: laravel sqlite sql-update phpunit laravel-5.3

我写了一个sql更新查询。它正在使用Mysql Workbench。我把sql update查询放在laravels db update中。当我用phpunit测试它时,我收到一个错误。 phpunit使用sqlite。

运行单元测试时出现以下错误:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 near "as": syntax error (SQL: UPDATE table1 as mainp LEFT JOIN ....)

当我删除"作为mainp"出现以下错误:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 near "LEFT": syntax error (SQL: UPDATE table1 as mainp LEFT JOIN ....)

这是整个更新查询

\DB::connection($this->dbConnection)->update(\DB::raw("
    UPDATE table1 as mainp left join
    (
        SELECT *
        FROM
        (
            select p.fullname, cc.surname as name_one, cc.forename as name_two
            from table1 as p
                LEFT JOIN table2 as cc
                on p.fullname LIKE CONCAT(cc.surname,'%')
                LEFT JOIN table2 as cc2
                on p.fullname LIKE CONCAT('%',cc.forename)
        ) AS pcccc
        WHERE fullname = CONCAT(name_one, name_two)
    ) AS main
    ON mainp.fullname = main.fullname
    SET mainp.name_one = main.name_one,
    mainp.name_two = main.name_two")
);

创建所有表格。当我使用db :: select时,我得到了一些结果。

return \DB::connection($this->dbConnection)->select(\DB::raw("select p.fullname, cc.surname as name_one, cc.forename as name_two
        from table1 as p
            LEFT JOIN table2 as cc
            on p.fullname LIKE CONCAT(cc.surname,'%')
            LEFT JOIN table2 as cc2
            on p.fullname LIKE CONCAT('%',cc.forename)"));

我做错了什么?

0 个答案:

没有答案