我正在尝试使用各自的元键更新数据库中的多个字段,但它不起作用。它仅在我一次更新一个时才有效。如何更新多个字段?
以下作品:
$sql = "UPDATE wp_usermeta SET meta_value = '$meta_value' WHERE meta_key = '$meta_key' AND user_id= $user_ID";
以下不起作用:
$sql = "UPDATE wp_usermeta SET (meta_value = '$meta_value', meta_key = '$meta_key'), (meta_value = 'The freak I wants', meta_key = 'on_the_weekends_i' WHERE user_id= $user_id)";
答案 0 :(得分:0)
请试试这个:
$metaInfo= [
[
'meta_key'=>$meta_key,
'meta_value'=>$meta_value,
],
[
'meta_key'=>'another key',
'meta_value'=>'another value',
],
[
'meta_key'=>'another key 1',
'meta_value'=>'another value 1',
]
];
foreach($metaInfo as $info){
foreach($info as $field => $value){
$sql= "UPDATE wp_usermeta SET ($field='$value') WHERE user_id=$user_id";
//execute here your query
}
}
答案 1 :(得分:0)
UPDATE wp_usermeta SET meta_value = CASE
WHEN user_id = 1 THEN 329152
WHEN user_id = 2 THEN 349325
WHEN user_id = 3 THEN 111592
ELSE meta_value
END
WHERE user_id in (1,2,3)
以下是如何在MySql中进行批量更新