mysql 批量更新多条记录

  众所周知,当我们想要替换某列的值为指定内容时,往往使用

"updata table set column = value"

那么问题来了,我想要把某列的内容更新为不同的值怎么办呢?

这样来干!

今天我更新数据表的某个字段的时间戳,

项目网址给的时间如下:

mysql 批量更新多条记录

 

但是我数据库的时间戳是前些天我录入的时间啊 ,怎么办,客户要求改过来。

那么……

我一一把网页上的时间用PHP转换

$res=array(
    0   => strtotime('2017-04-07'),
    1   => strtotime('2017-04-06'),
    ………………
    ………………
    38  =>strtotime('2017-01-12'),
    39  =>strtotime('2012-12-06'),
    40  =>strtotime('2012-03-01'),
);
foreach($res as $key => $val){
    echo $key.'-'.$val.'++++<br>';
}

然后重点来了,开始执行sql语句了!

update tp_news_content
    set create_at = case id 
        when 1 then 'value1'
        when 2 then 'value2'
        when 3 then 'value3'

        ……(以此类推)
    end

where id >7 and id <48

结束了!

当然  还可以增加字段哟!

update tp_news_content
    set create_at = CASE id 
        when 1 then 'value1'
        when 2 then 'value2'
        when 3 then 'value3'

        ……(以此类推)
    end

    title = case id

        when 1 then 'value1'
        when 2 then 'value2'
        when 3 then 'value3'

        ……(以此类推)
    end

where id >7 and id <48

 

这个意思呢就是当大条件 id大于7小于48的时候,

id=1 的时候 把create_at变为第一个值,

title 变为 指定的title的值

转载于:https://my.oschina.net/marhal/blog/1204444