If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Change values of 2 fields

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-09-07, 08:04
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
Change values of 2 fields

hi
i want to change values of 2 fiels in mysql, is there any function for it?
Reply With Quote
  #2 (permalink)  
Old 05-09-07, 08:18
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Take a look at this:
http://www.w3schools.com/sql/sql_update.asp
Let us know how you get on!
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 05-09-07, 09:55
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Do you mean change the datatype for the column in the table? or do you mean change the data values themselves. i.e. for record 10 change field1 (column1) to 'alpha' and field2(column2) to 'beta' ?
Reply With Quote
  #4 (permalink)  
Old 05-09-07, 12:52
bono56 bono56 is offline
Registered User
 
Join Date: May 2004
Posts: 133
Second one, suppose field1='a' & field2='b', i want to set field1='b' & field2='a'. & i cant add a third field for keeping value of one of them.
& some records values must change, so i cant rename column name.
in some programming language we have a function for change values of 2 variables, without making third variable, i just want to know is there such a function in mySQL?
Code:
var1 = 'a';
var2 = 'b';

//for changing value:
varTemp= var1;
var1 = var2;
var2 = varTemp;

unset VarTemp;

Last edited by bono56; 05-09-07 at 13:06.
Reply With Quote
  #5 (permalink)  
Old 05-09-07, 14:46
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by bono56
in some programming language we have a function for change values of 2 variables, without making third variable
use the same technique in sql
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 05-10-07, 03:33
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by bono56
2 variables, without making third variable
Code:
varTemp= var1;
var1 = var2;
var2 = varTemp;
I can see 3 variables in use
As Rudy suggests, the same can be achieved using SQL with no problems whatso-ever.
Variables in SQL: http://www.databasejournal.com/featu...le.php/3087431
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 05-10-07, 09:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
"change values of 2 variables, without making third variable"

you can do that with sql too
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 05-10-07, 09:28
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
The problem can even be solved with a single variable
1) declare your variable
2) assign the value of Field1 to the variable
3a) update Field1 with value from Field2
3b) update Field2 with value in our variable
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On