Ive tried to figure this one out. Selects I can use join. I have that figured out. But update? No clue. I tried using "IN" but im lost. if someone could push me in the right directrion it would be awesometastic. Trying to clear up buggy noob code /* *Subtracting Money from user A, Adding to user B *Simple money transfer script (fantasy money of course :P) *can this be done in 1 query? */ mysql_query("update users set money=money-'$amount' where id='$users[id]'") or die (mysql_error()); mysql_query("update users set money=money+'$amount' where id='$recieve_id[id]'") or die (mysql_error()); PHP:
Read some more about transactions with those you can make the assumption that all queries or none of the queries get COMMITED to the database.
To futher streess what "chisara" has pointed out. if you are really using this as displayed in your code sample for money, even "fantasy money" you must use a transaction with a rollback of it fails or if an error occured in the middle of it all
I told you how you can fix it, but ranabra's advice is a bit more important, he tells you that you must use transactions/rollbacks. You must use transactions when doing something like this.
You still can do JOIN's in UPDATE. Here's a example based on your UPDATE query: UPDATE users as a1, users as a2 SET a1.money = a1.money-$amount, a2.money = a2.money+$amount WHERE a1.id=$users[id] and a2.id=$recieve_id[id] Code (markup): PS. Don't forget to check if $users[id] even has the amount to send. You don't want his balance to go negative.
That looks charming greny, I even suspect that it will work, learn something new every day. But I have my doubts when you include the has the amount to send in the where clause. I suspect concurrent threads might produce a negative result but I suspect that 99,99% of the SQL users wouldn't be able to answer the safety of that construction with 100% certainty. With fully serialised transactions you could be sure of the consistency, but if a master of SQL could analyze this update join construction and answer I would be very gratefull to learn if this construction is "safe".