我想对我保存“用户元信息”的表执行更新查询。
我要更新“userid”=9对应的meta_value,其中可以有多个meta_key进行更新。 (这不是wordpress数据库)
我的表格如下所示:
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
请为此提出一些解决方案。
最佳答案
更新 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 "; };
您可以使用 CASE WHEN
或 IF( ... )
条件来更新列中的多行。
下面的例子应该对你有帮助。
使用IF(...)
函数:
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
使用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
<小时/>
您必须在 where 子句中使用 meta_key
为 meta_value
列设置新值。
update user_meta_info
set meta_value='new mobile number'
where
user_id = 9 and
meta_key = 'mobile'
如果每个 meta_key
可以有多个元值,则必须在 where 子句中使用 umeta_id
。如果 umeta_id
是主键,则可以省略在 where 子句中使用其他字段。
update user_meta_info
set meta_value='new mobile number'
where
umeta_id = 7 -- ( and user_id = 9 and meta_key = 'mobile' )
关于php - 更新 MySQL 中的 users_meta 中的表(不是 WordPress),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156514/