How to update and swap values using a query in mySQL

Aug 15, 2012 1 Comment by 1459 Views

While Im working on a certain project, I encounter a situation where I accidentally interchange the first name and the last name. Since the entries are quite big, It’s very hard to manually swap the values one by one. So, I search the web and found out the used of temporary variable in a query. Temporary variable in a query always start with an @ symbol followed by its variable name, then := to set a value.

The Solution:
In a query we need to keep the first name in a temporary variable, and interchange the last name and first name then get the last name from our temporary variable. Check the solution below:

UPDATE `user` SET `fname`=@tmp:=`fname`, `fname`=`lname`, `lname`=@tmp WHERE your_condition

Above is the final output of the solution, where the first name and last name swap successfully. Obviously there are other way to do it and you can freely share it using comment! Thank you!



Geeks, Programming

About the author

I'm a Freelance Web Developer (PHP, Flash, Ajax, Javascript, CSS & HTML 4/5), iOS Developer, SEO expert and a Graphic Designer from General Santos City. Freelancer.com Profile | Libnary.com | Pattern Lockscreen

One Response to “How to update and swap values using a query in mySQL”

Leave a Reply

DMCA.com Protection Status
© 2011 GensanBlog.com - All about General Santos City. Sitemap | Privacy Policy | Guidelines


Coded and Designed by Erwin Bantilan