【发布时间】:2015-08-15 03:53:20
【问题描述】:
我有两张这样的桌子:
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
我希望表 1 看起来像这样 ():
Description -- Price
Pineapple 25
Coke 5
Orange Juice 8
Milk 10
我已经尝试过了,但我只得到了选择查询返回的最后一个值(导致所有行都具有相同的值)。
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();}
谁能帮忙?非常感谢。
【问题讨论】:
-
我猜问题是您的更新会影响多行。这看起来真的很臭。这些表是如何关联的?他们不是以某种方式联系在一起吗?
标签: java sql sql-server-2008 sql-update