Jump to content

UPDATE with 2 SETs


fxuser

Recommended Posts

:(

 

$query1 = mysql_query("UPDATE act SET `from`='$new_un', `to`='$new_un' WHERE `to`='$un_curr'");

 

notice also the usage of backtics for your fields "from" and "to"... both are MYSQL rserved words

http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

 

Thanks for the reply but the query doesnt seem to work.. so i have to add these backtics in all my queries?will check the link , thanks.

Link to comment
https://forums.phpfreaks.com/topic/224971-update-with-2-sets/#findComment-1162041
Share on other sites

Thanks for the reply but the query doesnt seem to work.. so i have to add these backtics in all my queries?will check the link , thanks.

 

What happens?

 

it sents from and to WHERE `to`='$un_curr' which is not what i want..

i want it to from='$new_un' WHERE to='$un_curr'

and            to='$new_un' WHERE from='$un_curr'

 

would this work?

$query1 = mysql_query("UPDATE act SET `from`='$new_un', `to`='$new_un' WHERE `to`='$un_curr',`from`='$un_curr'");

 

so basically the first `from`='$new_un' goes with -> WHERE `to`='$un_curr'

and                          `to`='$new_un' goes with -> WHERE `from`='$un_curr'

Link to comment
https://forums.phpfreaks.com/topic/224971-update-with-2-sets/#findComment-1162067
Share on other sites

I think you want

 

$query1 = mysql_query("UPDATE act SET `from`='$new_un', `to`='$new_un' WHERE `to`='$un_curr' OR `from`='$un_curr'");

 

That will update both the 'from' and 'to' values on any row that has either 'to' or 'from' = $un_curr.

 

If you only want to update 'from' where 'from' = $un_curr, and only update 'to' where 'to' = $un_curr, then you do have to do it in two separate queries. (Well, you _can_ do it in one, but let's just say it's a lot easier to do it in two)

Link to comment
https://forums.phpfreaks.com/topic/224971-update-with-2-sets/#findComment-1162079
Share on other sites

on my table there might be some to columns NULL so i just want to update the from from un_curr to new_un ...

 

which causes problems... i can stick around with my 2 queries.. i dont wanna bother you much ... just want the merged query to do the same job as the 2 that i have on my first post.

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/224971-update-with-2-sets/#findComment-1162083
Share on other sites

@fxuser:  my fault... I read your post incorrectly

 

try this

UPDATE act
  SET   `from` = IF (`to` = '$un_curr', '$new_un', `from`),
        `to`   = IF (`from` = '$un_curr', '$new_un', `to`);

 

that did the job:

UPDATE act
  SET   `from` = IF (`from` = '$un_curr', '$new_un', `from`),
        `to`   = IF (`to` = '$un_curr', '$new_un', `to`);

 

thanks.

Link to comment
https://forums.phpfreaks.com/topic/224971-update-with-2-sets/#findComment-1162412
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.