doufen1890 2016-01-12 13: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-13 01:04
    关注

    I recommend using the following query

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

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面