I know this question is fairly similar to a few others that have been asked but I can't seem to find a solution while looking through their answers. Most of the questions that I could find dealt with delete queries and not update queries. I have been stuck on this small bug for a few days now and I'm hoping to get a quick answer without getting in trouble for asking this question. (I'm still a little new here so I apologize if I'm doing something very wrong). I'm trying to write a query that updates a checks someones inventory for an empty slot and adds an item to it. In this case an "empty slot" is when an itemID is equal to zero and a new item is any other number. Once this is completed the query will end. I've gotten some help writing the query and I believe it is close to being done but I have a small error that I can't seem to figure out.
Here's my code:
try{
//get connection to database
Connection con = DataBaseConnect.getConnection();
//create a statement
PreparedStatement addInfo = con.prepareStatement("UPDATE userinv SET "
+ "ItemID = "+i+" "
+ "WHERE 'Username' = '"+LoginController.userLog+"' "
+ "AND Slot = ("
+ "SELECT MIN(Slot) FROM userinv "
+ "WHERE 'ItemID' = 0 "
+ "AND 'Username' = '"+LoginController.userLog+"')");
//process result set
addInfo.executeUpdate();
}
catch(Exception e){
e.printStackTrace();
}
}
ERROR
You can't specify target table 'userinv' for update in FROM clause