SQLite select,join和where子句问题

时间:2013-11-15 13:34:50

标签: java android sql sqlite

我尝试使用join来填充包含来自3个表的数据的listview,并使用一个表来检查用户是否是组的一部分。但我很难获得给定用户的所有组。下面是桌子 组表

_id | group_name | group_description | group_image

用户表

_id | user_name | user_image

组参与者表(此表确定哪些用户属于哪些组)

_id | user_id_foreign | group_id_foreign

活动对象表

_id | target_id | user_id_foreign | target_type | time | type

这是我试图开始工作的sql语句:

Cursor c = database
            .rawQuery(
                    "SELECT groups.*, activityobject.time, activityobject.type, user.user_name, user.user_image "
                            + "FROM activityobject "
                            + "JOIN user "
                            + "ON user._id = activityobject.user_id "
                            + "JOIN groups"
                            + "ON groups._id = activityobject.target_id "
                            + "WHERE groups._id IN(SELECT group_id_foreign FROM group_participants WHERE user_id_foreign = ?)"
                            + "AND activityobject.target_type = 0 "
                            + "GROUP BY groups._id "
                            + "ORDER BY activityobject._id",
                    new String[] { String.valueOf(userId) });

我现在得到的结果是所有拥有activityobject的组,但没有没有的组。 我想获得给定用户的所有组,如果一个组有一个活动对象,我也想要来自该对象的数据。

提前致谢!

编辑: 我所参考的表格的图像 enter image description here 下面是表格中数据的插入语句

//Activity object table
    db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
            + KEY_ACTIVITYOBJECT_ID + ", "
            + KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
            + KEY_ACTIVITYOBJECT_USER_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
            + KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
            + KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
            + ") VALUES (1, 2, 1, 1, 'Alex gruppe', 0, '07-05-13', 1)");
    db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
            + KEY_ACTIVITYOBJECT_ID + ", "
            + KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
            + KEY_ACTIVITYOBJECT_USER_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
            + KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
            + KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
            + ") VALUES (2, 2, 1, 2, 'Jeremys gruppe', 0, '08-06-13', 2)");
    db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
            + KEY_ACTIVITYOBJECT_ID + ", "
            + KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
            + KEY_ACTIVITYOBJECT_USER_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
            + KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
            + KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
            + ") VALUES (3, 2, 1, 2, 'Jeremys gruppe', 1, '09-07-13',3)");
    db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
            + KEY_ACTIVITYOBJECT_ID + ", "
            + KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
            + KEY_ACTIVITYOBJECT_USER_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
            + KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
            + KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
            + ") VALUES (4, 2, 1, 2, 'Jeremys gruppe', 0, '10-08-13', 4)");
    db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
            + KEY_ACTIVITYOBJECT_ID + ", "
            + KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
            + KEY_ACTIVITYOBJECT_USER_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
            + KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
            + KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
            + ") VALUES (5, 2, 1, 1, 'Alex gruppe', 0, '11-09-13', 5)");
    db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
            + KEY_ACTIVITYOBJECT_ID + ", "
            + KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
            + KEY_ACTIVITYOBJECT_USER_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
            + KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
            + KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
            + ") VALUES (6, 2, 1, 1, 'Alex gruppe', 0, '12-10-13', 6)");
    db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
            + KEY_ACTIVITYOBJECT_ID + ", "
            + KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
            + KEY_ACTIVITYOBJECT_USER_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
            + KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
            + KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
            + ") VALUES (7, 2, 1, 1, 'Alex gruppe', 1, '13-11-13', 7)");
    db.execSQL("INSERT INTO " + TABLE_ACTIVITYOBJECT + " ("
            + KEY_ACTIVITYOBJECT_ID + ", "
            + KEY_ACTIVITYOBJECT_ACTIVITY_TYPE + ", "
            + KEY_ACTIVITYOBJECT_USER_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_ID + ", "
            + KEY_ACTIVITYOBJECT_TARGET_NAME + ", "
            + KEY_ACTIVITYOBJECT_TARGET_TYPE + ", "
            + KEY_ACTIVITYOBJECT_TIME + ", " + KEY_ACTIVITYOBJECT_OBJECT_ID
            + ") VALUES (8, 2, 1, 1, 'Alex gruppe', 0, '14-12-13', 8)");
    // Group table
    db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
            + KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
            + KEY_GROUP_IMAGE
            + ") VALUES (1,'alex gruppe', 'mega awesome gruppe', null);");
    db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
            + KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
            + KEY_GROUP_IMAGE
            + ") VALUES (2,'jeremy gruppe', 'mega awesome gruppe', null);");
    db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
            + KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
            + KEY_GROUP_IMAGE
            + ") VALUES (3,'ole gruppe', 'mega awesome gruppe', null);");
    db.execSQL("INSERT INTO " + TABLE_GROUP + " ( " + KEY_GROUP_ID + ", "
            + KEY_GROUP_NAME + ", " + KEY_GROUP_INFO + ", "
            + KEY_GROUP_IMAGE
            + ") VALUES (4,'egon gruppe', 'mega awesome gruppe', null);");
    // User table
    db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
            + KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
            + KEY_USER_STATUS + ") VALUES (1, 'Alex', null, 0)");
    db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
            + KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
            + KEY_USER_STATUS + ") VALUES (2, 'Peter', null, 0)");
    db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
            + KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
            + KEY_USER_STATUS + ") VALUES (3, 'Jeremy', null, 0)");
    db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
            + KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
            + KEY_USER_STATUS + ") VALUES (4, 'Søren', null, 0)");
    db.execSQL("INSERT INTO " + TABLE_USER + " (" + KEY_USER_ID + ", "
            + KEY_USER_NAME + ", " + KEY_USER_IMAGE + ", "
            + KEY_USER_STATUS + ") VALUES (5, 'Ole', null, 0)");

    // Group participants table
    db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
            + KEY_GROUP_PARTICIPANTS_ID + ", "
            + KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
            + KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(1, 1, 1)");
    db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
            + KEY_GROUP_PARTICIPANTS_ID + ", "
            + KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
            + KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(2, 1, 4)");
    db.execSQL("INSERT INTO " + TABLE_GROUP_PARTICIPANTS + " ( "
            + KEY_GROUP_PARTICIPANTS_ID + ", "
            + KEY_GROUP_PARTICIPANTS_USER_ID_FOREIGN + ", "
            + KEY_GROUP_PARTICIPANTS_GROUP_ID_FOREIGN + ") VALUES(3, 2, 2)");

1 个答案:

答案 0 :(得分:1)

“我想获得给定用户的所有组,如果一个组有一个活动对象,我也想要该对象的数据。”

然后你必须使用LEFT JOIN。

select g.*, a.time, a.type, u.user_name, u.user_image                             
from groups g
    join group_participants gp on gp.group_id_foreign = g._id
    left join activityobject a on g._id = a.target_id and a.target_type = 0
    left join user u on a.user_id_foreign = u.id
where gp.user_id_foreign = ?
order by a._id;

如果您只想选择每个组的最后一个活动对象(如果有),您也可以将其添加到连接条件中:

select g.*, a.*
from groups g
    join group_participants gp on gp.group_id_foreign = g._id
    left join activityobject a 
        on g._id = a.target_id and 
           a.target_type = 0 and 
           a._id in (select max(_id) from activityobject group by target_id)  
    left join users u on a.user_id_foreign = u.id
where gp.user_id_foreign = ?
order by a._id;

请记住,您必须将activityobjects的所有过滤条件添加到左连接的连接条件,而不是主查询的where子句。否则,您将从没有找到匹配的活动对象的结果中抑制行,而不是用空值填充列。