How do I merge two different tables to a single table?

淺唱寂寞╮ 提交于 2021-01-29 08:33:59

问题


I'm making an android-Base Point of Sale System and I'm straggling of combining two tables , I want The Table Foods and The Table Drinks are combined in the Table Menu.

public static final String DATABASE_NAME = "FOODs.db";
//FOODs Tables name
public static final String TABLE_FOODS = "table_Food";
public static final String TABLE_DRINKS = "table_drinks";
public static final String TABLE_MENU = "table_data";


//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 " +  TABLE_FOODS + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " NAME TEXT, PRICE TEXT)";
    db.execSQL(CreateTableMains);

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

    String CreateTable = "CREATE TABLE " +  TABLE_MENU + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            " NAME TEXT, PRICE TEXT)";
    db.execSQL(CreateTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOODS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_DRINKS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MENU);

回答1:


You can merge tables using UNION - see Compound Select Statements about 3/4 down between SELECTS

 SELECT * FROM table_Food UNION SELECT * FROM table_drinks

or

SELECT * FROM table_Food UNION ALL SELECT * FROM table_drinks

Both would work the same UNLESS you happened to have a drink and food that had the same name, the same price and the same id. Without ALL the duplicate rows would be omitted. As such UNION ALL is safer.

  • The above assumes that you want all columns.
  • Note the column counts from each select must match
    • but you can easily get around that e.g. SELECT *,1 as extracolumn FROM table_Food adds a new column to the result

IF what you want is to just use the one table table_data then to load the data from both the table_food AND the table_drinks table into the table_Data you could use :-

INSERT INTO table_data (name,price) SELECT name,price FROM table_Food UNION ALL SELECT name,price FROM table_Drinks;
  • id's are omitted as being a Primary Key if the values were the same then the above would fail due to a UNIQUE constraint conflict.

  • There will very likely be duplicate id's as you have INTEGER PRIMARY KEY for the id column. This makes the column an alias of the rowid column (a hidden column) which has the attribute of generating a value unique to the table initially 1, then 2 then 3 (but not guaranteed). The AUTOINCREMENT keyword adds an additional twist in that the unique value must be greater then any that exist or have been used.

If you want to temporarily merge all 3 tables then you could use:-

SELECT * FROM table_Food UNION ALL SELECT * FROM table_drinks UNION ALL SELECT * FROM table_Data;

If you wanted to merge all three and know what came from which table you could use :-

SELECT *,'Food' FROM table_Food UNION ALL SELECT *,'Drinks' FROM table_drinks UNION ALL SELECT *,'Menu' FROM table_Data;

SQL Fiddle demo of the above



来源:https://stackoverflow.com/questions/58361818/how-do-i-merge-two-different-tables-to-a-single-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!