簡體   English   中英

Java SQL從另一個表動態更新一個表,不帶id

[英]Java SQL update one table dynamically from another without id

我有兩個這樣的表:

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();}

有人可以幫忙嗎? 非常感謝你。

不知道您實際上要做什么。.但是更新一個表的基數很容易:

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

如果這不能回答您的問題,請舉一個示例,說明查詢前后兩個表的外觀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM