mysql组更新多个在哪里

时间:2014-02-14 03:09:02

标签: php mysql

我想多个更新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?

1 个答案:

答案 0 :(得分:1)

您可以使用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');