Stary12
Stary12

Reputation: 3

How i can get data from two tables in on query?

I want to get all data from two tables, and cast it into StringBuffer, but i have an issue with query. I don't know how i should call it properly.

That method will show user created by him training plans. There will be two kinds of this plan. For example, in FBW_TABLE_SET_A_CHEST user selected exercise named "squeezing the barbell", and in FBW_TABLE_SET_B_CHEST user has selected exercise named "squeezing the dumbbell".

Here is how tables are called

private static final String FBW_TABLE_NAME_SET_A = "FbwSetA";
private static final String FBW_TABLE_SET_A_CALF = "calfExerciseSetA";
private static final String FBW_TABLE_SET_A_LEGS = "legsExerciseSetA";
private static final String FBW_TABLE_SET_A_BACK = "backExerciseSetA";
private static final String FBW_TABLE_SET_A_SHOULDERS = "shouldersExerciseSetA";
private static final String FBW_TABLE_SET_A_BICEPS = "bicepsExerciseSetA";
private static final String FBW_TABLE_SET_A_TRICEPS = "tricepsExerciseSetA";
private static final String FBW_TABLE_SET_A_CHEST = "chestExerciseSetA";
private static final String FBW_TABLE_SET_A_ABS = "absExerciseSetA";
private static final String FBW_TABLE_SET_A_FOREARM = "forearmExerciseSetA";

private static final String FBW_TABLE_NAME_SET_B = "FbwSetB";
private static final String FBW_TABLE_SET_B_CALF = "calfExerciseSetB";
private static final String FBW_TABLE_SET_B_LEGS = "legsExerciseSetB";
private static final String FBW_TABLE_SET_B_BACK = "backExerciseSetB";
private static final String FBW_TABLE_SET_B_SHOULDERS = "shouldersExerciseSetB";
private static final String FBW_TABLE_SET_B_BICEPS = "bicepsExerciseSetB";
private static final String FBW_TABLE_SET_B_TRICEPS = "tricepsExerciseSetB";
private static final String FBW_TABLE_SET_B_CHEST = "chestExerciseSetB";
private static final String FBW_TABLE_SET_B_ABS = "absExerciseSetB";
private static final String FBW_TABLE_SET_B_FOREARM = "forearmExerciseSetB";

And here is how i "created" it by db.execSQL(..)

db.execSQL("create table " + FBW_TABLE_NAME_SET_A + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + FBW_TABLE_SET_A_CALF + " TEXT," + FBW_TABLE_SET_A_LEGS + " TEXT," + FBW_TABLE_SET_A_BACK + " TEXT," + FBW_TABLE_SET_A_SHOULDERS + " TEXT," + FBW_TABLE_SET_A_BICEPS + " TEXT," + FBW_TABLE_SET_A_TRICEPS + " TEXT," + FBW_TABLE_SET_A_CHEST + " TEXT," + FBW_TABLE_SET_A_ABS + " TEXT," + FBW_TABLE_SET_A_FOREARM + " TEXT)");
    db.execSQL("create table " + FBW_TABLE_NAME_SET_B + " (ID INTEGER PRIMARY KEY AUTOINCREMENT," + FBW_TABLE_SET_B_CALF + " TEXT," + FBW_TABLE_SET_B_LEGS + " TEXT," + FBW_TABLE_SET_B_BACK + " TEXT," + FBW_TABLE_SET_B_SHOULDERS + " TEXT," + FBW_TABLE_SET_B_BICEPS + " TEXT," + FBW_TABLE_SET_B_TRICEPS + " TEXT," + FBW_TABLE_SET_B_CHEST + " TEXT," + FBW_TABLE_SET_B_ABS + " TEXT," + FBW_TABLE_SET_B_FOREARM + " TEXT)");

In sum: that method should get all data from two tables, and cast in into StringBuffer and show user created by him training plans.

Upvotes: 0

Views: 55

Answers (2)

Pallavi Roy
Pallavi Roy

Reputation: 41

db.execSQL("SELECT * FROM " + FBW_TABLE_NAME_SET_A + 
"UNION" +
"SELECT * FROM " + FBW_TABLE_NAME_SET_B);

This should give you all non-duplicate rows of both the tables.

Upvotes: 0

jiwopene
jiwopene

Reputation: 3627

Use the UNION compound operator.

SQL code is something like this:

SELECT col1, col2 from table1 UNION SELECT col1, col2 from table2

Upvotes: 1

Related Questions