java - 如何根据同一数据库中的另一个值更改mysql中的值?
问题描述
我正在尝试根据同一数据库中的另一个值但不同的表来更改数据库中表中的一个值。第一个表称为订单,第二个表称为购买供应。我想要做的是我想通过从'orders'表中减去名为quantity_ordered的列中的值来更改'buysupply'表中的sumquantity的值。我尝试编写一些查询,但它不起作用,它不断弹出错误。如果您知道解决方案,请告诉我。代码也在下面
private void DispatchButtonActionPerformed(java.awt.event.ActionEvent evt) {
String type = txttype.getSelectedItem().toString();
String name = txtname.getText();
String quantity = txtquantity.getText();
String dispatch_row = txtdispatch.getText();
String statusDispatched = "Dispatched";
try {
Class.forName("com.mysql.jdbc.Driver");
con1 = DriverManager.getConnection("jdbc:mysql://localhost/restock", "root", "password");
// Focus on this
String template = "UPDATE orders SET status = '%s' WHERE id = %s";
String template2 = "UPDATE buysupply SET sumquantity = sumquantity - %s WHERE id = %s";
String quantity_ordered = "quantity_ordered FROM orders";
pst = con1.prepareStatement(String.format(template, statusDispatched, dispatch_row));
pst.executeUpdate();
pst1 = con1.prepareStatement(String.format(template2, quantity_ordered , dispatch_row));
pst1.executeUpdate();
// Look on top
JOptionPane.showMessageDialog(null, "Item has been dispatched");
// To update the newly recorded data to the table
table_update();
// Set the textfields to empty upon button click
txttype.setSelectedIndex(-1);
txtname.setText("");
txtquantity.setText("");
txtdispatch.setText("");
txttype.requestFocus();
} catch (ClassNotFoundException | SQLException ex) {
JOptionPane.showMessageDialog(null, "Quantity or Dispatch field is not an integer, Please try again.");
Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
}
}
// This code is in another class file
try {
Class.forName("com.mysql.jdbc.Driver");
con1 = DriverManager.getConnection("jdbc:mysql://localhost/restock", "root", "password");
String template = "SELECT SUM(quantity) as sumquantity FROM buysupply WHERE itemtype IN ('Plastic gloves', 'Rubber gloves')";
PreparedStatement pst = con1.prepareStatement(template);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
glovesum = rs.getString("sumquantity");
} else {
System.out.print("Query didn't return any results");
}
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(stock.class.getName()).log(Level.SEVERE, null, ex);
}
解决方案
据我了解(请阅读到最后),如果它是一个经典的发票订单数据库,其中包含产品列表的“buysupply”表和“订单”表中的产品订购客户列表。第一点,几个人进的评论点是两个表之间缺少的链接数据。我假设阅读您的代码和平,该链接是由一个 ID 建立的,但不清楚,所以我提供了一个基于这些列之间的链接的解决方案:
orders.itemtype = buysupply.itemtype
如果它在其他元素上,请将信息更改为下面的 SQL 查询。我还假设列 orders.status 将值从我所说的“等待”值更改为“分派”值。
所以这里将之前的数据放入“buysupply”表中:
id, itemtype, quantity
1 , mask , 704
2 , clothed, 101
3 , N95, 18
这里将之前的数据放入“订单”表中:
id, itemtype, quantity_orderred, status,
1,mask, 1 , Dispatched
2,clothed,3,Waiting
更新两个值(orders.status 和 buysupply.quantity)的 SQL 应该类似于假设要更新的 order.id 为 2:
update orders,buysupply
set orders.status='Dispatched',
buysupply.quantity = buysupply.quantity - orders.quantity_orderred
where
orders.itemtype = buysupply.itemtype
AND
orders.status = 'Waiting'
AND
orders.id = '2'
之后:所以这里的数据进入“buysupply”表:
id, itemtype, quantity
1 , mask , 704
2 , clothed, 98
3 , N95, 18
这里将之前的数据放入“订单”表中:
id, itemtype, quantity_orderred, status,
1,mask, 1 , Dispatched
2,clothed,3,Dispatched
更新可以应用于多个表和列,您应该只在每列中指明列表名称以避免混淆。
这可能是让您改进总和部分代码的第一步,恐怕根本不明白。
然后我找到一个部分信息,解释 sum_quantity 是从值的总和计算出来的,所以你不想改变数量,我的错。
所以你可以用这种 SQL 创建一个临时表,临时表在连接关闭时被销毁:
CREATE TEMPORARY TABLE IF NOT EXISTS TMPsumquantity AS
SELECT SUM(quantity) as sumquantity FROM buysupply WHERE itemtype IN ('Plastic gloves', 'Rubber gloves')
这可以创建一个包含您想要的信息的列,但是,据我所知,这不是我的建议;-)
我将创建一个新列来将总和值存储到表“buysupply”中,以说明“该订单将被调度时可用的库存数量是该元素的数量”所以你的总和值的结果
“buysupply”之前:id, itemtype, quantity, quantity_avalaible 1 , mask , 704, 704 2 ,cloned, 101, 101 3 , N95, 18, 18
“订单”前:id, itemtype, quantity_orderred, status, quantity_avalaible 1,mask, 1 , Dispatched 2,clothed,3, Waiting
所以创建此列的 SQL 很复杂,基于同一张表之间的内连接
UPDATE buysupply b1
INNER JOIN (
SELECT SUM(quantity) as sumquantity, id
FROM buysupply
where buysupply.itemtype IN ('clothed', 'N95')
) b2 ON true
SET b1.quantity_avalaible = b2.sumquantity
因此,新表“buysupply”的列“quantity_avalaible”包含 N95 的列数量值和服装值的总和:
id, itemtype, quantity, quantity_avalaible
1 , mask , 704, 116
2 , clothed, 101, 116
3 , N95, 18, 116
因此,您可以使用第一个 SQL 提议来更新 quantity_avalaible 取决于“orders.quantity_orderred”的值
最后一点,我对数据结构和业务逻辑有部分看法,将负值存储到“orders.quantity_orderred”列中可能很有用,因此 SQL SUM 可以通过对 SUM 的相同调用来添加和减去值功能
最好的
推荐阅读
- sql - 执行 max() 时 varchar 和 int 的区别?
- office-js - Office 插件清单中的 URL 查询参数
- algorithm - 指数与伪多项式时间
- asp.net - 如何解析 PE 文件以提取数字签名详细信息
- db2 - 如何将 VARGRAPHIC 传递给 DB2 中的存储过程?
- android - (Android 或 AndroidTV)当我们用 (1,1) 设置 setMeasuredDimension 时,我们得到了黑屏
- javascript - React JS - 如何优化渲染代码?
- c++ - 父类的继承和指向子类对象的指针
- go - 没有 fmt.Print() 光标移动不会发生
- java - Java Android room alter table java.lang.IllegalStateException: 迁移没有正确处理