How to join Two Sqlite table with different ID

342 views Asked by At

When I showed all stored data in my application the ID of two data in different tables are same.

enter image description here enter image description here

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "FOODs.db";
//FOODs Tables name
public static final String TABLE_FOODS = "table_Foods";
public static final String TABLE_DRINKS = "table_Drinks";
public static final String TABLE_MENU = "table_Menu";

//FOODs Table Columns name
private static final String COL_1 = "Primary ID";
private static final String COL_2 = "NAME";
private static final String COL_3 = "PRICE";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CreateTableMains = "CREATE TABLE IF NOT EXISTS " +  TABLE_FOODS + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " NAME TEXT, PRICE TEXT)";
    db.execSQL(CreateTableMains);

    String CreateTableDrinks = "CREATE TABLE IF NOT EXISTS " +  TABLE_DRINKS + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " NAME TEXT, PRICE TEXT)";
    db.execSQL(CreateTableDrinks);


public Cursor showMenuData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT  *,'Food' AS type FROM " + TABLE_FOODS +  " UNION SELECT *, 'Drink' AS type FROM " + TABLE_DRINKS, null);
    return data;
}
2

There are 2 answers

1
Moklesur Rahman On

Create a table variable and call the id with variable like..

String q= "SELECT a.id, a.name, b.price FROM table_a a INNER JOIN table_b b ON 
a.id=b.other_id WHERE b.property_id=?";
0
Swayangjit On

Try the following query

SELECT tf.*, td.*  FROM TABLE_FOODS tf LEFT JOIN TABLE_DRINKS td ON (tf.ID = td.ID);