[英]Update multiple rows in same table in different row
I have to update multiple rows in same table in different row as below table in single query. 我必须在同一表的不同行中更新多个行,如在单个查询中的下表所示。
Queries: 查询:
update util SET util_value='$util_value_desc' where util_head ='wc_contact_us'
update util SET util_value='$util_value_email' where util_head ='wc_email'
update util SET util_value='$util_value_mobile' where util_head ='wc_mobile'
update util SET util_value='$util_value_map' where util_head ='wc_google_map'
also for select query to get data in textfield 也用于选择查询以获取文本字段中的数据
$query=mysqli_query($connect,"SELECT util_value FROM util where util_head='wc_contact_us'");
$row=mysqli_fetch_array($query);
$util_value=$row['util_value'];
$util_value_email=$row['util_value'];
$util_value_map=$row['util_value'];
$util_value_mobile=$row['util_value'];
You could use an update with a CASE
expression: 您可以使用带有
CASE
表达式的更新:
UPDATE util
SET util_value = CASE util_head WHEN 'wc_contact_us' THEN ?
WHEN 'wc_email' THEN ?
WHEN 'wc_mobile' THEN ?
WHEN 'wc_google_map' THEN ? END
WHERE
util_head IN ('wc_contact_us', 'wc_email', 'wc_mobile', 'wc_google_map');
To the four placeholders ?
给四个占位符
?
you would bind the following four PHP variables: 您将绑定以下四个PHP变量:
$util_value_desc
$util_value_email
$util_value_mobile
$util_value_map
But this is a fairly verbose query, and you might want to stick with separate updates, maybe within a single transaction. 但这是一个相当冗长的查询,您可能希望坚持单独的更新,也许在单个事务中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.