doufen1890 2016-01-12 05:28
浏览 55
已采纳

在一个查询mysql中预先添加和替换

I'm explaining with an example for getting my solution. I've a user table having id, name, image fields.

Sample record should be,

id - 1
name - TestUser
image - temp/testuser.jpg (sometimes no sub-directory 'temp'. ie, image - testuser.jpg)

After user registers, I'm moving the image to a new subdirectory.

ie, moving from temp/testuser.jpg(testuser.jpg) to 1/testuser.jpg

So I need to update with the new url.

ie, I need two operations on here. Replace the sub-directory temp with 1, if no sub-directory prepend '1/' to the existing image url.

How can I manage these operations in one query? Kindly help me :)

  • 写回答

2条回答 默认 最新

  • dongsi0625 2016-01-12 17:04
    关注

    I recommend using the following query

    update users set
    image = concat('new_dir/', substring(image from locate('/', image) + 1))
    where id = 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部