1

I know similar questions exist in which the user is trying to retrieve key generated of new row and the same thing as i want in PHP but i want to know its alternate for java. This is what i want:

String query = "UPDATE table SET tabValue = (tabValue+number) WHERE id="+xyz;

I want to retrieve the tabValue after it is being updated in the same query! Is it possible or do i have to write another query for it? Can anyone help me out here please? NOTE: I am using JDBC.

10
  • 3
    WHERE id="+xyz SQL injection here. Commented May 26, 2015 at 9:11
  • Its just an example. I could do it in a safe way actually. Commented May 26, 2015 at 9:12
  • 1
    Why can't you just use tabValue+number in your Java code? Commented May 26, 2015 at 9:14
  • 1
    phpMyAdmin is the administration tool. you are using MySQL. I think you have to use a StoredProcedure for your solution. Commented May 26, 2015 at 9:15
  • 1
    In java you can get AUTO_INCREMENT field (dev.mysql.com/doc/connector-j/en/…), but not an any field from insert/update statement. Commented May 26, 2015 at 9:27

1 Answer 1

1

From the MySQL documentation:

Currently, you cannot update a table and select from the same table in a subquery.

You cannot refer to the table which is being updated in a FROM clause.

You are better off just keeping track of what the new value will be after UPDATE in your Java code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.