SQLITE CREATE VIEW JOIN的语法问题

时间:2015-02-17 15:55:02

标签: android mysql sqlite syntax

当我尝试将3张桌子连在一起时,在CREATE VIEW和JOIN中遇到一些麻烦,我可以做2但是3似乎对我来说不合适。我是新手,所以我只是通过反复试验,因为我所经历的大多数文件都没有真正涵盖(据我的理解)加入3个表格而且#&#&# 39;不太顺利,所以我在这里提出一些建议,希望能有一个解决方案。提前谢谢。

以下是表格:

条款

db.execSQL("CREATE TABLE " + termsTable + " (" + colTermsID + " INTEGER PRIMARY KEY , " + colTermsClass + " TEXT)");

状态

  db.execSQL("CREATE TABLE " + statTable + " (" + colStatusID + " INTEGER PRIMARY KEY , " + colStatClass + " TEXT)");

帐户

db.execSQL("CREATE TABLE " + accountsTable + " (" + colID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            colName + " TEXT, " +
            colAmount + " Integer, " +
            colPurpose + " TEXT, " +
            colTerms +" INTEGER NOT NULL, " +
            colStatus +" INTEGER DEFAULT '1'," +
            colDate + " TEXT, " +
            "FOREIGN KEY (" + colTerms + ") REFERENCES " + termsTable + " (" + colTermsID + ") " + "," +
            "FOREIGN KEY (" + colStatus + ") REFERENCES " + statTable + " (" + colStatusID + "));");

这是我正在使用的创建视图,只使用其中一个条款或状态,但当我尝试同时使用两者时都没有,所以我认为我在那个区域做错了。

    db.execSQL("CREATE VIEW " + viewAccs +
                    " AS SELECT " + accountsTable + "." + colID + " AS _id," +
                    " " + accountsTable + "." + colName + "," +
                    " " + accountsTable + "." + colAmount + "," +
                    " " + accountsTable + "." + colPurpose + "," +
                    " " + termsTable + "." + colTermsClass + "" +
                    " FROM " + accountsTable + " JOIN " + termsTable + " ON " + accountsTable + "." + colTerms + " = " + termsTable + "." + colTermsID +
                    " " + statTable + "." + colStatClass + "" +
                    " FROM " + accountsTable+ " JOIN " + statTable + " ON " + accountsTable + "." + colStatus + " = " + statTable + "." + colStatusID +
                    " " + accountsTable + "." + colDate
    );

编辑(在此答复):

这是我在修复语法并遵循X.L Ant提到的内容后最终得到的实际代码。

            db.execSQL("CREATE VIEW " + viewAccs +
                    " AS SELECT " + accountsTable + "." + colID + " AS _id," +
                    " " + accountsTable + "." + colName + "," +
                    " " + accountsTable + "." + colAmount + "," +
                    " " + accountsTable + "." + colPurpose + "," +
                    " " + termsTable + "." + colTermsClass + "," +
                    " " + statTable + "." + colStatClass + "," +
                    " " + accountsTable + "." + colDate + "" +
                    " FROM " + accountsTable +
                    " JOIN " + termsTable + " ON " + accountsTable + "." + colTerms + " = " + termsTable + "." + colTermsID +
                    " JOIN " + statTable + " ON " + accountsTable + "." + colStatus + " = " + statTable + "." + colStatusID
    );

1 个答案:

答案 0 :(得分:0)

一个查询中不能有两个FROM

试试这个:

db.execSQL("CREATE VIEW " + viewAccs +
                " AS SELECT " + accountsTable + "." + colID + " AS _id," +
                " " + accountsTable + "." + colName + "," +
                " " + accountsTable + "." + colAmount + "," +
                " " + accountsTable + "." + colPurpose + "," +
                " " + termsTable + "." + colTermsClass + "" +
                " FROM " + accountsTable + 
                " JOIN " + termsTable + " ON " + accountsTable + "." + colTerms + " = " + termsTable + "." + colTermsID +
                " " + statTable + "." + colStatClass + "" +
                " JOIN " + statTable + " ON " + accountsTable + "." + colStatus + " = " + statTable + "." + colStatusID +
                " " + accountsTable + "." + colDate

确保您在SQL中注入的变量是安全的,以避免注入。