Java SQL 从另一个没有 id 的动态更新 table
Java SQL update one table dynamically from another without id
我有两个这样的 table:
Table 1:
Description -- Price
Apple 10
Wine 20
Coffe 5
Table 2:
Description -- Price -- Number
Pineapple 25 1
Coke 5 1
Orange Juice 8 2
Milk 10 3
我希望 table 1 看起来像这样 ():
Description -- Price
Pineapple 25
Coke 5
Orange Juice 8
Milk 10
我试过了,但我只得到了 select 查询返回的最后一个值(导致所有行都具有相同的值)。
PreparedStatement ps = con.prepareStatement("update Table1 set Description=?, Price=? WHERE (DATEPART(year, date) = "+arraydate[0]+"\n"+
"AND DATEPART(month, date) ="+arraydate[1]+"\n"+
"AND DATEPART(day, date) ="+arraydate[2]+")");
try {
st=con.createStatement();
ResultSet rs= st.executeQuery("select Description,Price from Table2 where number in(35,36)");
while(rs.next()){
String desc=rs4.getString("Description");
Double price=rs4.getDouble("Price");
ps.clearParameters();
ps.setString(1,desc);
ps.setDouble(2,price);
ps.executeUpdate();
}
}
catch(Exception ex)
{ex.printStackTrace();}
有人可以帮忙吗?非常感谢。
不确定您实际上要做什么..但是更新一个 table 基础到另一个很容易:
Update A
Set A.Price = B.Price
FROM Table1 as A
INNER JOIN Table2 AS B
ON A.Name = B.Name
and A.Description = B.Description
如果这不能回答您的问题,请举例说明您希望查询前后的两个 table 是什么样子。
我有两个这样的 table:
Table 1:
Description -- Price
Apple 10
Wine 20
Coffe 5
Table 2:
Description -- Price -- Number
Pineapple 25 1
Coke 5 1
Orange Juice 8 2
Milk 10 3
我希望 table 1 看起来像这样 ():
Description -- Price
Pineapple 25
Coke 5
Orange Juice 8
Milk 10
我试过了,但我只得到了 select 查询返回的最后一个值(导致所有行都具有相同的值)。
PreparedStatement ps = con.prepareStatement("update Table1 set Description=?, Price=? WHERE (DATEPART(year, date) = "+arraydate[0]+"\n"+
"AND DATEPART(month, date) ="+arraydate[1]+"\n"+
"AND DATEPART(day, date) ="+arraydate[2]+")");
try {
st=con.createStatement();
ResultSet rs= st.executeQuery("select Description,Price from Table2 where number in(35,36)");
while(rs.next()){
String desc=rs4.getString("Description");
Double price=rs4.getDouble("Price");
ps.clearParameters();
ps.setString(1,desc);
ps.setDouble(2,price);
ps.executeUpdate();
}
}
catch(Exception ex)
{ex.printStackTrace();}
有人可以帮忙吗?非常感谢。
不确定您实际上要做什么..但是更新一个 table 基础到另一个很容易:
Update A
Set A.Price = B.Price
FROM Table1 as A
INNER JOIN Table2 AS B
ON A.Name = B.Name
and A.Description = B.Description
如果这不能回答您的问题,请举例说明您希望查询前后的两个 table 是什么样子。