为什么我的Select语句不返回任何数据?

时间:2016-04-20 18:52:14

标签: android sql sqlite select join

我用外键创建了两个表。数据插入成功,但是当我使用SELECT语句时,没有显示数据。代码如下。

public static final String CREATE_TABLE_BLOCK_LIST = "CREATE TABLE " + TABLE_BLOCK_LIST
            + "(" + ID_BLOCK_LIST + " INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT 0,"
            + ORIGINATING_ADDRESS + " TEXT)";

public static final String CREATE_TABLE_BLOCKED_SMS = "CREATE TABLE " + TABLE_BLOCKED_SMS
            + "(" + ID_BLOCKED_SMS + " INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT 0, "
            + MESSAGE_BODY + " TEXT,"
            + FK_ID_BLOCK_LIST + " INTEGER," +
            "FOREIGN KEY(" + FK_ID_BLOCK_LIST + ") REFERENCES " + TABLE_BLOCK_LIST + "(" + ID_BLOCK_LIST +"))";

SELECT语句

SQLiteDatabase db = this.getWritableDatabase();
            //cursor = db.rawQuery("SELECT * FROM " + TABLE_BLOCKED_SMS, null);
            cursor = db.rawQuery("SELECT * FROM table_block_list " +
                    "JOIN table_blocked_sms " +
                    "ON table_block_list.id_block_list = table_blocked_sms.id_blocked_sms" , null);

请指导我解决问题的所在。

1 个答案:

答案 0 :(得分:0)

您的加入是隐式INNER JOIN

如果您的第一个表有数据而第二个表没有数据,则INNER JOIN将不返回任何数据。

您需要做的是使用LEFT OUTER JOIN

SELECT * FROM table_block_list
    LEFT OUTER JOIN table_blocked_sms
        ON table_blocked_sms.id_blocked_sms = table_block_list.id_block_list