1

I have two tables which I want to update using an SQL Update statement. These two tables contains a row named "oak" and "exp" which is the values I want to update. My best guess so far is this:

'UPDATE `items_woods`, `skills_woodcutting` SET `oak`=`oak`+1, `exp`=`exp`+13 WHERE `id`=?'

However, none of the values are saved in the database. Thanks in advance.

UPDATE:

if ($stmt = $mysqli->prepare('

        BEGIN TRANSACTION

        UPDATE items_woods
        SET items_woods.oak = ´1´
        FROM items_woods T1, skills_woodcutting T2
        WHERE T1.id = T2.id 
        and T1.id = ´?´

        UPDATE skills_woodcutting
        SET skills_woodcutting.exp = ´1´
        FROM items_woods T1, skills_woodcutting T2
        WHERE T1.id = T2.id
        and T1.id = ´?´

        COMMIT


        ')) {
user3287771
  • 113
  • 2
  • 8
  • Is your `WHERE` clause actually finding anything to update? –  Feb 16 '14 at 21:46
  • Use a join if you want to UPDATE 2 tables – Mihai Feb 16 '14 at 21:46
  • @MikeW yes, I am using prepared statements, and the questionmark is set to be 1. I tested the update statement with only updating one value and it worked. – user3287771 Feb 16 '14 at 21:48
  • @Mihai how would I implement it on my example? Thanks – user3287771 Feb 16 '14 at 21:51
  • 1
    possible duplicate of [How to update two tables in one statement in SQL Server 2005?](http://stackoverflow.com/questions/2044467/how-to-update-two-tables-in-one-statement-in-sql-server-2005) – TFD Feb 16 '14 at 21:52
  • `update t1 JOIN t2 ON t1.somecolumn=t2.someothercolumn SET t1.col=x,t2.col=y` This is for mysql – Mihai Feb 16 '14 at 21:54

2 Answers2

1

SQL Update, will only update one table at a time

Use a transaction with two update statements

TFD
  • 23,890
  • 2
  • 34
  • 51
  • can you help me with the code above? I think I did precisely what was described in the other thread but it still does not work. Thanks. – user3287771 Feb 16 '14 at 22:22
  • @user3287771 Did it do anything? Did you get error message? YOu have two ? marks, need to pass two parameters (same value) – TFD Feb 16 '14 at 22:24
  • It did nothing but returning an error message that I have created myself if the statement doesn't go through. The two questionmarks should both be equal to 1 so I guess that should be fine. – user3287771 Feb 16 '14 at 22:29
0

As there two tables, you must update them with two separate statements. It is possible to make these a single transaction, and committing both transactions only after both are successful.

I'm not sure what datbase you are working with, but the general approach would be:

commit   (start the transaction)
update items_woods set oak = oak+1 where id = ?
update skills_woodcutting set exp=exp+13 where id = ?
commit   (complete the transaction)
port5432
  • 5,889
  • 10
  • 60
  • 97
  • Your SQL is wrong, use BEGIN TRANSACTION, not COMMIT to start a transaction. Anyway it's a duplpicat equestion – TFD Feb 16 '14 at 21:53
  • In ORACLE you use a COMMIT or ROLLBACK to begin the transaction. As I mentioned in the post, the original poster did not specify the database. Thanks. – port5432 Feb 17 '14 at 07:23
  • The first COMMIT just empties the already running (implicit or named) transaction. A new transaction is then started automatically with the very next statement. COMMIT does not actually start a transaction (ANSI or Oracle) – TFD Feb 17 '14 at 08:24
  • That's correct, the COMMIT effectively clears uncommited transactions. It is commonly used as a starting point for a new transaction. Rollback has the same effect, but of course reverses any uncommited work. Oracle applications use this technique extensively. So my SQL was not wrong, just another way to do the same thing as begin_transaction. Cheers. – port5432 Feb 17 '14 at 08:57
  • While it works for you, it is not a good programming practice. Forcing acceptance and of close current transactions, without checking? Your sample code labels the command as if it starts a new transaction, it doesn't. It closes the current transaction. The next issued command starts a new implicit transaction – TFD Feb 17 '14 at 09:02
  • All of Oracle Ebusiness and PeopleSoft Applications are written this way. It is very rare to see a begin_transaction in their code. I agree with what you are saying, but am also pointing out that using a commit works and is commonly used. – port5432 Feb 17 '14 at 10:06
  • PHP is commonly used too :-P – TFD Feb 17 '14 at 10:41