如何将2个表中的SQL列相乘并插入单独的列中?

达斯汀·哈德斯佩斯

我正在尝试编写一个SQL Android应用程序,该应用程序从一张表中获取库存数量,然后将其乘以另一张表中商品的价格,以将总计存储到另一列中。根据我所阅读的内容,这似乎很简单,但对我却不起作用。我已经尝试了多种解决方案,但是到现在为止,我所做的是将SQL代码放在一个称为totalPrice()的单独方法中,该方法将在另一个方法中单击按钮时被调用。

public static final String TABLE_INVOICE = "Invoice";
public static final String INVOICE_ID = "Invoice_ID";
public static final String INVOICE_DATE = "Date";

public static final String TABLE_INVENTORY = "Inventory";
public static final String INVENTORY_ID = "Inventory_ID";
public static final String INVENTORY_ITEM = "Item";
public static final String INVENTORY_XS = "extraSmall";
public static final String INVENTORY_S = "Small";
public static final String INVENTORY_M = "Medium";
public static final String INVENTORY_L = "Large";
public static final String INVENTORY_XL = "extraLarge";
public static final String INVENTORY_XXL = "extraExtraLarge";
public static final String INVENTORY_PRICE = "Price";

public static final String TABLE_ORDERED = "Ordered";
public static final String ORDERED_ID = "Ordered_ID";
public static final String ORDERED_ITEM = "Item";
public static final String ORDERED_SIZE = "Size";
public static final String ORDERED_QUANTITY = "Quantity";
public static final String ORDERED_TOTAL = "Total";

public DbHelper(Context context){
    super(context, DB_NAME, null, DB_VERSION);
}

public void onCreate(SQLiteDatabase db) {
    String CREATE_CUSTOMER_TABLE = "CREATE TABLE " + TABLE_CUSTOMER + " ("
            + C_CUSTOMER_ID + " INTEGER PRIMARY KEY, "
            + C_CUSTOMER_NAME + " TEXT, "
            + C_EMAIL + " TEXT, "
            + C_STREET_ADDRESS + " TEXT, "
            + C_CITY + " TEXT, "
            + C_STATE + " TEXT, "
            + C_ZIP + " TEXT, "
            + C_PHONE + " TEXT "
            + ")";

    String CREATE_INVENTORY_TABLE = "CREATE TABLE " + TABLE_INVENTORY + " ("
            + INVENTORY_ID + " INTEGER PRIMARY KEY, "
            + INVENTORY_ITEM + " TEXT, "
            + INVENTORY_XS + " TEXT, "
            + INVENTORY_S + " TEXT, "
            + INVENTORY_M + " TEXT, "
            + INVENTORY_L + " TEXT, "
            + INVENTORY_XL + " TEXT, "
            + INVENTORY_XXL + " TEXT, "
            + INVENTORY_PRICE + " REAL "
            + ")";

    String CREATE_INVOICE_TABLE = "CREATE TABLE " + TABLE_INVOICE + " ("
            + INVOICE_ID + " INTEGER PRIMARY KEY, "
            + INVOICE_DATE + " DATETIME, "
            + C_CUSTOMER_ID + " INTEGER, "
            + "FOREIGN KEY(" + C_CUSTOMER_ID + ") REFERENCES " + TABLE_CUSTOMER + " (" + C_CUSTOMER_ID + "));";

    String CREATE_ORDERED_TABLE = "CREATE TABLE " + TABLE_ORDERED + " ("
            + ORDERED_ID + " INTEGER PRIMARY KEY, "
            + ORDERED_ITEM + " TEXT, "
            + ORDERED_SIZE + " TEXT, "
            + ORDERED_QUANTITY + " INTEGER, "
            + ORDERED_TOTAL + " REAL, "
            + INVENTORY_ID + " INTEGER, "
            + "FOREIGN KEY(" + INVENTORY_ID + ") REFERENCES " + TABLE_INVENTORY + " (" + INVENTORY_ID + "));";

    db.execSQL(CREATE_CUSTOMER_TABLE);
    db.execSQL(CREATE_INVENTORY_TABLE);
    db.execSQL(CREATE_INVOICE_TABLE);
    db.execSQL(CREATE_ORDERED_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CUSTOMER);

    //create tables again
    this.onCreate(db);
}

public Cursor totalPrice()
{
    //open database
    SQLiteDatabase db = this.getWritableDatabase();

    Cursor res = db.rawQuery("SELECT " + ORDERED_QUANTITY + ", " + INVENTORY_PRICE + ", " +
            ORDERED_QUANTITY + " * " + INVENTORY_PRICE + " AS " + ORDERED_TOTAL +
            " FROM " + TABLE_ORDERED + " INNER JOIN " + TABLE_INVENTORY +
            " ON Ordered." + INVENTORY_ID + " = Inventory." + INVENTORY_ID, null);
    return res;
}

我还有另一个类,可以在按钮单击内调用该方法。

public class AddInvoice extends AppCompatActivity {
Button addInvoice;
DbHelper db;
EditText date, cid, item, size, invID, quantity;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_invoice);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    db = new DbHelper(this);

    addInvoice = (Button) findViewById(R.id.btnAddToInvoice);
    date = (EditText) findViewById(R.id.etDate);
    cid = (EditText) findViewById(R.id.etCID);
    item = (EditText) findViewById(R.id.ItemName);
    size = (EditText) findViewById(R.id.etSize);
    invID = (EditText) findViewById(R.id.etAddInventoryID);
    quantity = (EditText) findViewById(R.id.etQuantity);

    addInvoice.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
              AddInvoice();
              AddOrderedItem();
              db.totalPrice();
              date.setText("");
              cid.setText("");
              item.setText("");
              size.setText("");
              invID.setText("");
              quantity.setText("");
                }
            }
    );
              getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          }

public void AddOrderedItem() {

    boolean isInserted = db.addOrderedItem(item.getText().toString(), size.getText().toString(), Integer.parseInt(invID.getText().toString()),
            Integer.parseInt(quantity.getText().toString()));
    db.totalPrice();
        if (isInserted == true)
            Toast.makeText(AddInvoice.this, "Item Inserted", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(AddInvoice.this, "Item not Inserted", Toast.LENGTH_SHORT).show();
        }

public void AddInvoice(){
    boolean isInserted = db.addInvoice(date.getText().toString(), cid.getText().toString());
        if (isInserted == true)
            Toast.makeText(AddInvoice.this, "Invoice Inserted", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(AddInvoice.this, "Invoice not Inserted", Toast.LENGTH_SHORT).show();
}

}

现在,当我运行代码时,“总价格”列中存储的值为空。谢谢你的帮助。

rxn1d

我假设您基本上想执行以下INNER JOIN查询:

SELECT column1 * column2 AS result FROM table1 t1 
  INNER JOIN table2 t2 dt ON t1.key = t2.key

然后,您可以在Java代码中自由使用此值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将 2 个熊猫数据框中的 2 列彼此相乘

如何将excel中的1列拆分为2个单独的列

加入同一个表时,如何将加入的数据放在单独的行而不是单独的列中?

SQL Server:如何解析一个列的值并插入到单独的表的多个列中?

如何将值插入另一个表的列中?

如何将两个SQL表中的所有值相乘和求和

如何将 JSON 数组转换为单独的值以插入到我的 SQL 表中?

将两个不同表中的两列相乘后找到最大值-SQL

用linq to sql将两个表中的两列相乘

SQL:将两列(来自不同的表)相乘并插入/更新(其中一个表)

如何将数据框中的特定列与同一数据框中的一个特定列相乘?

在处理列的其余部分时,如何将导致错误的数据插入单独的文件中?

如何将多个不同的列从源表插入到目标表中的一列

如何将DataFrame中的特定列插入数据库表的新列中?

如何将父表中的数据列插入到子类的列中

如何将两个sql表中的行和列合并到一个表中

如何将DataFrame中列的所有值相乘而不是仅根据位置相乘?

如何将一列添加到SQL表中,该表是另外两个列的乘积?

SQL将表中的行插入到表中,该表必须具有2个不同的列,但也必须具有一个非不同的列

将单独的熊猫数据框中的列相乘

如何将另一个选择的结果插入添加了额外列值的新表中?

如何将查询的结果扩展到 SQL 中的单独列中?

如何将单独列中冒号前后的单词拆分为sql中的行

如何将基于日期的用户特定信息加载到SQL中单独的日期列中

SQL Server - 如何将散列密码插入表?

MySQL:通过将位于两个单独表中的价格和数量列相乘来计算饭店数据库中每个订单的总价

SQL-将2个表中的2个值相乘以显示新表的结果

如何将两个select语句合并到一个具有两个单独列的表中?

如何将一个表中的多行插入到另一个表的单行的结构列中?