为什么我在 "WHERE" 附近的查询中出现语法错误? (多个表)

Why am I having an syntax error in my query near "WHERE"? (Multiple tables)

我对此还是很陌生,这是我第一次使用这些多表查询。为什么我有这样的错误?这是我的代码:

 String selectQuery =
            " select *" +
            "    from tableassign left outer join\n" +
            "         tableacc\n" +
            "         on tableassign.signeeid = tableacc.userid left outer join\n" +
            "         tableinfo\n" +
            "         on tableassign.signeeid = tableinfo.userid left outer join\n" +
                    " WHERE tableassign.signeedepid =?";
    Cursor data = db.rawQuery(selectQuery , new String[]{ signeedep });

错误:

android.database.sqlite.SQLiteException: near "WHERE": syntax error (code 1):
 , while compiling: select *  from tableassign left outer join  tableacc on
 tableassign.signeeid = tableacc.userid left outer join  tableinfo on 
tableassign.signeeid = tableinfo.userid left outer join WHERE 
tableassign.signeedepid =?

您似乎遇到了 copy/paste 问题。删除 where 之前的最后一个 left outer join:

String selectQuery =
        " select *" +
        "    from tableassign left outer join\n" +
        "         tableacc\n" +
        "         on tableassign.signeeid = tableacc.userid left outer join\n" +
        "         tableinfo\n" +
        "         on tableassign.signeeid = tableinfo.userid\n" +
                " WHERE tableassign.signeedepid =?";
Cursor data = db.rawQuery(selectQuery , new String[]{ signeedep });

我会以不同的方式格式化它,主要是为了便于阅读:

String selectQuery =
        " SELECT *\n" +
        " FROM tableassign\n" +
        " LEFT JOIN tableacc ON tableassign.signeeid = tableacc.userid\n" +
        " LEFT JOIN tableinfo ON tableassign.signeeid = tableinfo.userid\n" +
        " WHERE tableassign.signeedepid =?";
Cursor data = db.rawQuery(selectQuery , new String[]{ signeedep });

虽然我不确定您在查询中使用的是什么结构化语言,但问题似乎出在您对 JOIN 运算符的使用上。 LEFT OUTER JOIN 上的 Oracle 文档显示了它的正确用法,其中的语法是:

TableExpression LEFT [ OUTER ] JOIN TableExpression
{
    ON booleanExpression |
    USING clause
}

最后的 LEFT OUTER JOIN 紧接在 WHERE 子句之前,没有定义 TableExpression。删除 LEFT OUTER JOIN 子句以消除错误。

 String selectQuery =
            " select *" +
            "    from tableassign left outer join\n" +
            "         tableacc\n" +
            "         on tableassign.signeeid = tableacc.userid left outer join\n" +
            "         tableinfo\n" +
            "         on tableassign.signeeid = tableinfo.userid" +
                    " WHERE tableassign.signeedepid =?";
    Cursor data = db.rawQuery(selectQuery , new String[]{ signeedep });