sqlite JOIN query for multiple entries - java

My database has "diary" table and "food" table. In diary table I have following columns: date, food1,food2, ...food10. In food table I have: id, name, allergen1, allergen2, allergen3. So basically you log up to 10 foods a day to diary. Now I'm trying to build a query which would let me find last date that I've eaten food with allergen3. So that's the query I came up with, which you probably know is not working ;-)
Cursor findDateRes = db.rawQuery("SELECT date(" + DIARY_COL_DATE + ") FROM " + DIARY_TABLE
+ " INNER JOIN " + FOOD_TABLE + " food1 ON (food1." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F1 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food2 ON (food2." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F2 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food3 ON (food3." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F3 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food4 ON (food4." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F4 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food5 ON (food5." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F5 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food6 ON (food6." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F6 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food7 ON (food7." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F7 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food8 ON (food8." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F8 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food9 ON (food9." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F9 + ")"
+ " INNER JOIN " + FOOD_TABLE + " food10 ON (food10." + FOOD_COL_1 + " = " + DIARY_TABLE + "." + DIARY_COL_F10 +
") WHERE 1 NOT IN (food1." + FOOD_COL_5 +
",food2." + FOOD_COL_5 +
",food3." + FOOD_COL_5 +
",food4." + FOOD_COL_5 +
",food5." + FOOD_COL_5 +
",food6." + FOOD_COL_5 +
",food7." + FOOD_COL_5 +
",food8." + FOOD_COL_5 +
",food9." + FOOD_COL_5 +
",food10." + FOOD_COL_5 +
") ORDER BY date(" + DIARY_COL_DATE + ") ASC LIMIT 1", null);
"FOOD_COL_1" stands for id and "FOOD_COL_5" stands fro allergen3.
Thank you for looking. Any input will be appreciated.

Related

avoid adding the same element if exist in the list instead just add the elements inside the existing list [duplicate]

This question already has an answer here:
Add item to arraylist if it does not already exist in list
(1 answer)
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Below is the sample code
String jsonString = "{\n" +
" \"models\":[\n" +
" {\n" +
" \"model\":{\n" +
" \"code\":\"ALL\",\n" +
" \"type\":null,\n" +
" \"name\":\"ALL\",\n" +
" \"feature_types\":null\n" +
" }\n" +
" },\n" +
" {\n" +
" \"model\":{\n" +
" \"code\":\"102e\",\n" +
" \"defaultLookup\":\"false\",\n" +
" \"type\":\"SIT\",\n" +
" \"name\":\"MUSTANG\",\n" +
" \"feature_types\":[\n" +
" {\n" +
" \"feature_type\":{\n" +
" \"code\":\"A\",\n" +
" \"desc\":\"All feature types\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"feature_type\":{\n" +
" \"code\":\"B\",\n" +
" \"desc\":\"Series\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"feature_type\":{\n" +
" \"code\":\"C\",\n" +
" \"desc\":\"BodyStyle\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"model\":{\n" +
" \"code\":\"980p\",\n" +
" \"defaultLookup\":\"false\",\n" +
" \"type\":\"SIT\",\n" +
" \"name\":\"Ranger\",\n" +
" \"feature_types\":[\n" +
" {\n" +
" \"feature_type\":{\n" +
" \"code\":\"C\",\n" +
" \"desc\":\"All feature types\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"feature_type\":{\n" +
" \"code\":\"D\",\n" +
" \"desc\":\"Series\"\n" +
" }\n" +
" } \n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"model\":{\n" +
" \"code\":\"kkpou\",\n" +
" \"defaultLookup\":\"false\",\n" +
" \"type\":\"UAT\",\n" +
" \"name\":\"Transit Custom\",\n" +
" \"feature_types\":[\n" +
" {\n" +
" \"feature_type\":{\n" +
" \"code\":\"F\",\n" +
" \"desc\":\"All feature types\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"feature_type\":{\n" +
" \"code\":\"G\",\n" +
" \"desc\":\"Series\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"feature_type\":{\n" +
" \"code\":\"H\",\n" +
" \"desc\":\"Payload\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
for(int i = 0; i<myData.size();i++)
{
String type = "SIT";
FeaturedItems item = resultList.stream().filter(featureItem -> type != null && type.equals(featureItem.getType())).findFirst().orElse(null);
if (type != null) {
item = FeaturedItems.builder().type(type).items(new ArrayList<>()).build();
resultList.add(item);//if the item already exists in the list don't add the new item, instead just add the elements in the exisiting item.
//tried the below commented code to add the item if it doesn't contain in the list -- start
/*boolean flagFound = false;
for (FeaturedItems featureItem : resultList) {
if (featureItem.getType().equalsIgnoreCase(type)) {
flagFound = true;
break;
}
}
if(!flagFound) resultList.add(item);*/
//tried the above commented code to add the item if it doesn't contain in the list -- End
for (int count = 0; count < features.size(); count++) {
String id = getFid(count);
MyDataBuild build = ....//logic to set values in the properties
item.getItems().add(build);
}
}
}
lookUpData.setFeatureGroups(resultList);
}
}
If the type value is already defined in the defined featureItems, then instead of creating the new object in the featureItems list, i need to add the unique items(desc,id) to the existing items element for the matching type. The code snippet mentioned above doesn't add the elements to the existing items if the type is matching in the featureItems list, instead it is creating the new element as shown in the output json sample.
Using a Map instead will make your live much easier. However your example is missing some data so it's a bit hard to understand what is actually happening in your code. So I can give you only a simple example for the usage.
Map<String, FeaturedItems> resultMap = new HashMap<>();
// Get the FeaturedItems for the given type. If none is present create a new one.
FeaturedItems items = resultMap.computeIfAbsent(type, k -> FeaturedItems.builder().type(k).items(new ArrayList<>()).build());
// Add your item to the list
Sale newItem // Obtain new item
items.getItems().add(newItem);

Parse list of list to a JSON object

I have the following data, which is a list of lists:
"segmentation": [[239.97,260.24,222.04,270.49,199.84,253.41,213.5,227.79,259.62,200.46,274.13,202.17,277.55,210.71,249.37,253.41,237.41,264.51,242.54,261.95,228.87,271.34]]
What I need to do is to parse the information to a JSON object without removing the second braces.
I tried it with Jackson, but this fails with any data types.
Do you have any idea how to handle this?
Parse to JsonNode will work. I think u try with invalid json. check:
String value = "{\n" +
" \"segmentation\": [\n" +
" [\n" +
" 239.97,\n" +
" 260.24,\n" +
" 222.04,\n" +
" 270.49,\n" +
" 199.84,\n" +
" 253.41,\n" +
" 213.5,\n" +
" 227.79,\n" +
" 259.62,\n" +
" 200.46,\n" +
" 274.13,\n" +
" 202.17,\n" +
" 277.55,\n" +
" 210.71,\n" +
" 249.37,\n" +
" 253.41,\n" +
" 237.41,\n" +
" 264.51,\n" +
" 242.54,\n" +
" 261.95,\n" +
" 228.87,\n" +
" 271.34\n" +
" ]\n" +
" ]\n" +
"}";
JsonNode jsonNode = new ObjectMapper().readTree(value);

Hql Query.nor working

I need to write the query according to this logic
LeadRepository.getQualifiedLeadsWithoutClosedWonOrLost to the following query:
select l.id from prospectr360.lead l, prospectr360.lead_action la where l.id = la.lead_id and la.action_id = 6 and l.id not in (select l.id from prospectr360.lead_action la, prospectr360.action a, prospectr360.lead l where la.action_id = a.id and la.lead_id = l.id and reason_id in (61,65))
and I have written
#Query("SELECT lead.id " +
" FROM Lead lead, " +
" LeadAction la, " +
" WHERE lead.id = leadAction.lead.id"+
" AND leadAction.action.id = 6" +
" WHERE NOT EXISTS (SELECT 1 " +
" FROM Lead lead, " +
" Action action, " +
" WHERE action.id = la.action.id" +
" AND la.lead.id = lead.id" +
" AND la.reason.id in (61,65)" +
")")
List<Lead> getQualifiedLeadsWithoutClosedWonOrLost();
}
I resolved it
#Query("SELECT lead " +
" FROM Lead lead, " +
" LeadAction leadAction " +
" WHERE lead.id = leadAction.lead.id"+
" AND leadAction.action.id = 6" +
" AND lead.id NOT IN (SELECT lead1.id " +
" FROM Lead lead1, " +
" LeadAction la ," +
" Action action " +
" WHERE la.action.id = action.id" +
" AND la.lead.id = lead1.id" +
" AND la.reason.id in (61,65)" +
")")
List<Lead> getQualifiedLeadsWithoutClosedWonOrLost();

"No such column" when running update on SQLite

I'm trying to update a row in my database. Here is the code I am trying to use to update:
public void addFBComments(int id){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS+"="+"HELLO" + " WHERE " + COL_FB_ID+"="+id;
db.execSQL(query);
}
When I run the command, it throws this error:
08-18 15:42:10.145: E/AndroidRuntime(10493): FATAL EXCEPTION: Thread-922
08-18 15:42:10.145: E/AndroidRuntime(10493): android.database.sqlite.SQLiteException: no such column: HELLO (code 1): , while compiling: UPDATE fb_feed SET fb_comments= HELLO WHERE _id=3
Here is the TABLE_FB_FEED:
private static final String TABLE_FB_FEED_CREATE = "create table " + TABLE_FB_FEED +
" ("
+ COL_FB_ID + " integer primary key autoincrement not null,"
+ COL_FB_MESSAGE + " text,"
+ COL_FB_CAPTION + " text,"
+ COL_FB_POSTER_ID + " text,"
+ COL_FB_POST_ID + " text,"
+ COL_FB_LIKES + " text,"
+ COL_FB_POSTER_NAME + " text,"
+ COL_FB_COMMENTS + " text," // this is the column i want to update
+ COL_FB_LIKES_NUM + " text,"
+ COL_FB_TIMESTAMP + " text,"
+ COL_FB_PROFILE_PICTURE_URI + " text,"
+ COL_FB_PICTURE_URI + " text,"
+ COL_FB_NAME + " text,"
+ COL_FB_PREVIOUS_PAGE_URI + " text,"
+ COL_FB_NEXT_PAGE_URI + " text,"
+ COL_FB_POST_TYPE + " text,"
+ COL_FB_LINK_NAME + " text,"
+ COL_FB_COMMENTS_NUM + " text,"
+ COL_FB_STORY + " text,"
+ COL_FB_DESCRIPTION + " text,"
+ COL_FB_COMMENTS_BEFORE + " text,"
+ COL_FB_COMMENTS_AFTER + " text,"
+ COL_FB_LIKES_PROFILE_PIC + " text,"
+ COL_FB_COMMENTS_PROFILE_PIC + " text);";
Why is it throwing the query? I thought I was doing everything correctly.
in your code you need to add single quotes:
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS
+"="+"'HELLO'" + " WHERE " + COL_FB_ID+"="+id
or
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS
+"='HELLO' WHERE " + COL_FB_ID+"="+id
this is another example
"UPDATE DB_TABLE SET YOUR_COLUMN='newValue' WHERE id=myidValue");
In SQL, string values must be quoted:
String query = "..." + COL_FB_COMMENTS+"= 'HELLO' WHERE ...";
Anyway, formatting problems such as this (which get worse when the string itself contains quotes) can be avoided by using parameters:
String query = "... SET " + COL_FB_COMMENTS+"= ? WHERE " + COL_FB_ID+"="+id;
db.execSQL(query, new Object[] { "HELLO" });
You need to escape your String literal,
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS
+"="+"HELLO" + " WHERE " + COL_FB_ID+"="+id
Could be
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS
+"="+"'HELLO'" + " WHERE " + COL_FB_ID+"="+id
Or, you could use a bind parameter.
something like
String query = "UPDATE " + TABLE_FB_FEED + " SET " + COL_FB_COMMENTS+"="+"'HELLO'" + " WHERE " + COL_FB_ID+"="+id;
would work

SQLite select, join and where clause problems

Im trying to populate a listview with data from 3 tables using join, and one table to check wether a user is part of a group. But im having trouble getting all the groups for a given user. Heres the tables
Group table
_id | group_name | group_description | group_image
User table
_id | user_name | user_image
Group participants table (this table determines which users are part of which groups)
_id | user_id_foreign | group_id_foreign
Activityobject table
_id | target_id | user_id_foreign | target_type | time | type
Here is the sql statement im trying to get to work:
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) });
The result i get right now are all the groups that has an activityobject, but not the ones without.
I would like to get all groups for a given user, and if a group has an activityobject i want data from that object aswell.
Thanks in advance!
EDIT:
An image of the tables im refering to
Heres my insert statements with data in the tables
//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)");
"I would like to get all groups for a given user, and if a group has an activityobject i want data from that object aswell."
You have to use a LEFT JOIN then.
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;
If you just want to select the last activity object per group (if any), you can add that to the join condition, too:
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;
Just keep in mind that you have to add all filtering criteria for activityobjects to the join condition of the left join, not to the where clause of your main query. Otherwise, you will surpress rows from the result where no matching activityobject can be found, instead of filling up the columns with nulls.

Categories

Resources