繁体   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