英文:
How to change value in mysql based on another value in the same database?
问题
以下是您要翻译的代码部分:
我试图根据同一数据库中不同表中的另一个值来更改数据库中表中的一个值。第一个表称为“orders”,第二个表称为“buysupply”。我想要做的是通过从“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");
// 焦点在这里
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();
// 在上面查看
JOptionPane.showMessageDialog(null, "Item has been dispatched");
// 更新表中的新记录数据
table_update();
// 按钮单击时将文本字段设置为空
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);
}
}
// 这段代码在另一个类文件中
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);
}
请注意,我只翻译了您提供的代码部分,并没有添加额外的内容。如果您有其他问题或需要进一步的帮助,请随时提出。
英文:
I am trying to change one of the value in my table in database based on an another value in the same database but different table. The first table is called orders and the second is called buy supply. What I want to do is I want to change the value of sumquantity that is in 'buysupply' table by subtracting a value from the column named quantity_ordered from 'orders' table. I tried writing some query but it is not working, it keeps popping up error. If you know of a solution, do let me know. The code is all below as well
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);
}
答案1
得分: 1
根据我理解的内容(请读到最后),如果这是一个经典的发票订单数据库,其中包含“buysupply”表和“order”表,用于订单客户的产品列表。
首先,评论中的一些人提到了两个表之间缺少链接数据。根据我阅读的代码片段,我假设链接是通过一个ID进行的,但这并不清楚,所以我提供了一个基于这些列之间链接的解决方案:
orders.itemtype = buysupply.itemtype
如果是其他元素,请更改下面的SQL查询中的信息。
我还假设“orders.status”列需要更改值,从我称之为“等待”的值更改为“已发货”的值。
因此,这是“buysupply”表中的数据之前:
id,itemtype,quantity
1,口罩,704
2,衣服,101
3,N95口罩,18
这是“order”表中的数据之前:
id,itemtype,quantity_orderred,status,
1,口罩,1,已发货
2,衣服,3,等待
用于更新这两个值(orders.status和buysupply.quantity)的SQL应该类似于以下内容,假设要更新的order.id为2:
update orders,buysupply
set orders.status='已发货',
buysupply.quantity = buysupply.quantity - orders.quantity_orderred
where
orders.itemtype = buysupply.itemtype
AND
orders.status = '等待'
AND
orders.id = '2';
之后,“buysupply”表中的数据如下:
id,itemtype,quantity
1,口罩,704
2,衣服,98
3,N95口罩,18
这是“order”表中的数据之后:
id,itemtype,quantity_orderred,status,
1,口罩,1,已发货
2,衣服,3,已发货
更新可以应用于多个表和列,您只需要在每个列旁边指示列表名称,以避免混淆。
这可以是让您改进代码的第一步,我担心完全不理解的部分是求和部分。
然后我发现部分信息解释了sum_quantity是从值的总和计算出的计算值,因此您不想更改数量,我的错。
所以您可以使用这种SQL创建一个临时表,临时表在连接关闭时被销毁:
CREATE TEMPORARY TABLE IF NOT EXISTS TMPsumquantity AS
SELECT SUM(quantity) as sumquantity FROM buysupply WHERE itemtype IN ('塑料手套', '橡胶手套')
这可以创建一个包含您想要的信息的列,但是,据我了解,这不是我的建议
我将创建一个新列来存储表“buysupply”中的总和值,以表示“此订单将在发货时可用的库存数量为该元素的数量”,因此您的总和值的结果
在“buysupply”之前:
id,itemtype,quantity,quantity_avalaible
1,口罩,704,704
2,衣服,101,101
3,N95口罩,18,18
在“order”之前:
id,itemtype,quantity_orderred,status,quantity_avalaible
1,口罩,1,已发货
2,衣服,3,等待
因此,创建此列的SQL比较复杂,基于同一表之间的内部连接:
UPDATE buysupply b1
INNER JOIN (
SELECT SUM(quantity) as sumquantity, id
FROM buysupply
where buysupply.itemtype IN ('衣服', 'N95口罩')
) b2 ON true
SET b1.quantity_avalaible = b2.sumquantity
因此,新表“buysupply”中有包含数量列的“quantity_avalaible”列,该列包含N95和衣服值的数量列的总和:
id,itemtype,quantity,quantity_avalaible
1,口罩,704,116
2,衣服,101,116
3,N95口罩,18,116
然后,您可以根据“orders.quantity_orderred”的值使用第一个SQL建议来更新“quantity_avalaible”。
最后一点,我对数据结构和业务逻辑有一个部分了解,将负值存储到列“orders.quantity_orderred”中可能会有用,因此SQL SUM可以使用相同的SUM函数调用添加和减去值。
最好的祝愿!
英文:
As far I understand (and please read until the end) if it is a classic Invoice order database with a list of product into "buysupply" table and "order" table for the ordering client list of product.
The first point, several people into the comment point is the missing link data between both table. I assume reading your peace of code that the link is made by a ID but that not clear, so I offer a solution base on a link between those columns:
orders.itemtype = buysupply.itemtype
If it on an other element, please change the information into the SQL query below.
I assume also the column orders.status as to change the values from what I call 'Waiting' value to 'Dispatched' value.
So here the data before into "buysupply" table:
id, itemtype, quantity
1 , mask , 704
2 , clothed, 101
3 , N95, 18
Here the data before into "order" table:
id, itemtype, quantity_orderred, status,
1,mask, 1 , Dispatched
2,clothed,3,Waiting
The SQL to update both value (the orders.status and buysupply.quantity ) should be something like that suppose the order.id to update is 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'
AFTER:
So here the data after into "buysupply" table:
id, itemtype, quantity
1 , mask , 704
2 , clothed, 98
3 , N95, 18
Here the data before into "order" table:
id, itemtype, quantity_orderred, status,
1,mask, 1 , Dispatched
2,clothed,3,Dispatched
The update could apply on several tables and columns, you should just indicate the column table name with each column to avoid confusion.
That could be the first step to let you improve the code for the sum part, that, I afraid no understand at all.
Then I find a partial information explaining that the sum_quantity is a computed calcul from a sum of the value, so you do not want to change the quantity, my bad.
So you can create a temporary table with this kind of SQL, temporary table is detroy at the connection close:
CREATE TEMPORARY TABLE IF NOT EXISTS TMPsumquantity AS
SELECT SUM(quantity) as sumquantity FROM buysupply WHERE itemtype IN ('Plastic gloves', 'Rubber gloves')
That could create a column with the information you want, BUT, it's not my recommendation as far I understand
I will create a new column to store the sum value into the table "buysupply", to say the "the quantity in stock avalaible at the moment this order will be Dispatched is that for this element" so the result of you sum value
Before "buysupply":
id, itemtype, quantity, quantity_avalaible
1 , mask , 704, 704
2 , clothed, 101, 101
3 , N95, 18, 18
Before "order":
id, itemtype, quantity_orderred, status, quantity_avalaible
1,mask, 1 , Dispatched
2,clothed,3, Waiting
So the SQL to create this column is complex, base on an inner-join between the same table
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
So the new table "buysupply" with the colum "quantity_avalaible" containing the sum of the value of the colum quantity for N95 and clothed values :
id, itemtype, quantity, quantity_avalaible
1 , mask , 704, 116
2 , clothed, 101, 116
3 , N95, 18, 116
So then you can use the first SQL proposal to update quantity_avalaible depending the value of "orders.quantity_orderred"
Last point, I have a partial view on the data structure and the bussiness logic, it could be usefull to store a negative value into the column "orders.quantity_orderred" so the SQL SUM could add and substract values with the same call to the SUM function
Best
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论