Results 1 to 8 of 8
  1. #1
    Join Date
    May 2004
    Posts
    144

    Unanswered: Change values of 2 fields

    hi
    i want to change values of 2 fiels in mysql, is there any function for it?

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Take a look at this:
    http://www.w3schools.com/sql/sql_update.asp
    Let us know how you get on!
    George
    Home | Blog

  3. #3
    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' ?

  4. #4
    Join Date
    May 2004
    Posts
    144
    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 14:06.

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    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
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    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
    Home | Blog

  7. #7
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    "change values of 2 variables, without making third variable"

    you can do that with sql too
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  8. #8
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    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
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •