簡體   English   中英

mysql組更新多個

[英]mysql group update with multiple where

我想多個更新mysql字段。

<?php
    if (isset($_POST['sb'])) {
        $site_title = mysql_real_escape_string($_POST['site_title']);
        $site_url = mysql_real_escape_string($_POST['site_url']);
        $admin_email = mysql_real_escape_string($_POST['admin_email']);
        $allowed_extension = mysql_real_escape_string($_POST['allowed_extension']);
        $crawler_status = mysql_real_escape_string($_POST['crawler_status']);

        if (mysql_query("update shop_option set shop_field ='$site_title' where shop_key='site_title'"))
            print 'updated';
        else 
            echo mysql_error();
    }
?>

在上面的代碼中,我只能更新一個字段。 我需要運行多個更新,這是我的更新規則:

update shop_option set shop_field ='$site_url' where shop_key='site_url'
update shop_option set shop_field ='$admin_email' where shop_key='admin_email'
update shop_option set shop_field ='$allowed_extension' where shop_key='allowed_extension'
update shop_option set shop_field ='$crawler_status' where shop_key='crawler_status'

當每個Where不同時,如何多次更新mysql?

您可以使用casein

update shop_option
    set shop_field = (case when shop_key = 'site_url' then '$site_url'
                           when shop_key = 'admin_email' then '$admin_email'
                           when shop_key = 'allowed_extension' then '$allowed_extension'
                           when shop_key = 'crawler_status' then '$crawler_status'
                      end)
    where stop_key in ('site_url', 'admin_email', 'allowed_extension', 'crawler_status');

編輯:

除上述查詢中的拼寫錯誤外(很可能),以下內容是否返回任何內容?

select *
from shop_option
where stop_key in ('site_url', 'admin_email', 'allowed_extension', 'crawler_status');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM