从另一个表更新整个列

时间:2016-01-27 15:11:38

标签: android sqlite

我有两张表,表1

Table1

和表2

Table 2

我正在尝试将列rawQty从表2复制到表1,但它只复制表的第一行,复制行中的相同项。

我尝试了两个查询,但两者都给出了相同的结果。这是我的代码:

public void copyItemToReset(){

    SQLiteDatabase db = MmpDBHelper.this.getWritableDatabase();

    String sql1 = "UPDATE " + ITEM_DETAIL_TABLE + " SET " +ITEMDETAIL_RAWQTY + "= ( "  + " SELECT " + ITEM_REFRESH_RAWQTY + " FROM " + ITEM_REFRESH_TABLE+") WHERE EXISTS " +
        "( SELECT * FROM " + ITEM_REFRESH_TABLE + " WHERE " +ITEM_DETAIL_TABLE+"."+ITEMDETAIL_BOMNO + "="+ITEM_REFRESH_TABLE+ "." +ITEM_REFRESH_BOMNO+")";
        String sql2 = "UPDATE " + ITEM_DETAIL_TABLE + " SET " +ITEMDETAIL_FINIQTY + "= ( "  +" SELECT " + ITEM_REFRESH_FINIQTY + " FROM " + ITEM_REFRESH_TABLE+")";
        String sql3 = "UPDATE " + ITEM_DETAIL_TABLE + " SET " +ITEMDETAIL_QTYUMUSED + "= ( "  +" SELECT " + ITEM_REFRESH_QTYUMUSED + " FROM " + ITEM_REFRESH_TABLE+")";

    try {  
        Log.i("sql1=", sql1);
        Log.i("sql2=", sql2);
        Log.i("sql3=", sql3);

        db.execSQL(sql1);
        db.execSQL(sql2);
        db.execSQL(sql3);
    } catch (Exception exe) {
        exe.printStackTrace();
        db.endTransaction();
        Log.e("Insertion failed", "Transaction failure when inserting itemdet data.");
        this.closeDatabase();
        Log.i("DB closed", "Database closed successfully and Copied Successfully");
        errCode = "Err-DB-06";
        LogFileCreator.appendLog(errCode + " : " + exe.getMessage());
    }
}

1 个答案:

答案 0 :(得分:1)

子查询必须是correlated subquery,它只返回一个值,但每次执行都会返回一个值:

UPDATE ItemDetail
SET rawQty = (SELECT rawQty
              FROM ItemRefresh
              WHERE bomNo = ItemDetail.bomNo
                AND item  = ItemDetail.item);