0

i want to perform update query a table where i am holding "User Meta Info".

I want to update the meta_value corresponds to "userid" = 9, in which multiple meta_key can be there to update. (This is not wordpress database)

My table looks like this:

umeta_id     userid      meta_key       meta_value
 1              9         mobile        123324
 2              9         address       some address
 3              9         city          some city
 4              9         country       some country
 5              9         occupation    some details
 6              9         website       someurl
 7              9         mobile        123324
 8              9         address       some address
 9              9         city          some city
 10             10        country       some country
 11             10        occupation    some details
 12             10        website       someurl

Please suggest some solution for this.

user3201500
  • 1,538
  • 3
  • 22
  • 43

1 Answers1

1

Update 1:

If i want to update multiple values, which can be any meta_key which is not right now, but can be there. i am using PHP to update this my function looks like this

public static function update
  ( $fullname, $mobile, $interest, $occupation,
    $address, $city, $country, $about, $‌​website ) {
  $sql = "update user_meta SET ";
};

You can use either a CASE WHEN or a IF( ... ) condition to update multiple rows of a column.

Following example should help you.

Using IF(...) function:

update user_meta_info
  set
     meta_value = if( meta_key='mobile', '$mobile', meta_value ),
     meta_value = if( meta_key='address', '$address', meta_value ),
     meta_value = if( meta_key='city', '$city', meta_value ),
     meta_value = if( meta_key='country', '$country', meta_value ),
     meta_value = if( meta_key='occupation', '$occupation', meta_value ),
     meta_value = if( meta_key='website', '$website', meta_value )
where
  user_id = 9

Using CASE ... WHEN ...:

update user_meta_info
  set
     meta_value = case meta_key
                        when 'mobile' then '$mobile'
                        when 'address' then '$address'
                        when 'city' then '$city'
                        when 'country' then '$country'
                        when 'occupation' then '$occupation'
                        when 'website' then '$website'
                        else meta_value
                  end
where
  user_id = 9

You have to use meta_key in where clause to set a new value for meta_value column.

update user_meta_info
 set meta_value='new mobile number'
where
 user_id = 9 and
 meta_key = 'mobile'

And in case if each meta_key can have multiple meta values, then you must use umeta_id in where clause. And if umeta_id is a primary key, you can omit using other fields in where clause.

update user_meta_info
 set meta_value='new mobile number'
where
 umeta_id = 7 -- ( and user_id = 9 and meta_key = 'mobile' )
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • If i want to update multiple values, which can be any meta_key which is not right now, but can be there. i am using PHP to update this my function looks like this public static function `update($fullname,$mobile,$interest,$occupation,$address,$city,$country,$about,$website){ $sql = "update user_meta SET "; }`; – user3201500 Jan 16 '14 at 09:24
  • please check this link too, is possible. http://stackoverflow.com/questions/21157511/select-query-in-two-tables-in-mysql. Someone already answered but looking for more dynamic way. in which i am not restrict with meta_key, means any number of meta key can be generated into coloum from row. thank you! once again. – user3201500 Jan 16 '14 at 13:04
  • @user3201500: Yes. That solution is pivoting the data. Where as in this `update ...` it is done in a similar way. – Ravinder Reddy Jan 16 '14 at 13:11