[英]Update multiple rows in same table in different row
我必須在同一表的不同行中更新多個行,如在單個查詢中的下表所示。
查詢:
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'
也用於選擇查詢以獲取文本字段中的數據
$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'];
您可以使用帶有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');
給四個占位符?
您將綁定以下四個PHP變量:
$util_value_desc
$util_value_email
$util_value_mobile
$util_value_map
但這是一個相當冗長的查詢,您可能希望堅持單獨的更新,也許在單個事務中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.