【问题标题】:Updating all rows within a single column using phpMyAdmin使用 phpMyAdmin 更新单个列中的所有行
【发布时间】:2015-09-13 19:32:24
【问题描述】:

请有人帮我写一个 SQL 语句,我可以用它来使用PHPMyAdmin 更新我的 MySQL 数据库中的一个表?

背景

我有一个名为 XYZ 的数据库和一个名为 v2rns_content

的表

该表有许多列,其中一列称为images

images列的每一行都有相同的内容,只是文件路径不同例如:

{
   "image_intro":"images\/news_images\/Logos\/Logo.png",
   "float_intro":"",
   "image_intro_alt":"",
   "image_intro_caption":"",
   "image_fulltext":"",
   "float_fulltext":"",
   "image_fulltext_alt":"",
   "image_fulltext_caption":""
}

其中 images/news_images/Logos/Logo.png 是文件路径。

我想通过添加一个额外的级别来更新这个文件路径,例如

images/news_images/legacy/Logos/Logo.png

问题:
因此,是否可以在我的图片列中搜索文本 images/news_images 并用 images/news_images/legacy 替换此列中的所有行?

注意事项:
值得注意的是,v2rns_content 表被证明难以导出(由于大小和特殊字符),否则我只需在 excel 中查找和替换,然后使用 phpMyAdmin 再次导入。

v2rns_content 表包含超过 20,000 条记录

我不是 SQL 方面的专家,所以如果我能够使用 phpMyAdmin 中的 SQL 函数实现这一点,我将不胜感激(如果可能的话)。

提前感谢您的帮助。非常感谢。

【问题讨论】:

  • images 唯一要更新的列吗?
  • 是的。我希望其他一切都保持不变。

标签: mysql phpmyadmin


【解决方案1】:

没有很好地了解您的架构。为了让您使用该值更新所有列(相同或不同,取决于您在 PHP 中对它的处理方式)。

这将替换整个值:

UPDATE v2rns_content SET image_intro ='images\/news_images\/legacy' 
WHERE image_intro LIKE '%images\/news_images%'

如果您想替换部分目录路径,您可能需要这样做:

UPDATE v2rns_content 
SET image_intro = REPLACE(image_intro , 'images\/news_images', 'images\/news_images\/legacy')

这是官方参考页面:http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_replace

【讨论】:

  • 如果我现有的内容是{ "image_intro":"images\/news_images\/Logos\/Logo.png", "float_intro":"", "image_intro_alt":"", "image_intro_caption":"", "image_fulltext":"", "float_fulltext":"", "image_fulltext_alt":"", "image_fulltext_caption":"" },这段代码会不会改成:{ "image_intro":"images\/news_images\/legacy\/\/Logos\/Logo.png", "float_intro":"", "image_intro_alt":"", "image_intro_caption":"", "image_fulltext":"", "float_fulltext":"", "image_fulltext_alt":"", "image_fulltext_caption":"" }
  • 这真的取决于你如何存储它。我不确定在将其保存到数据库之前如何或是否将其转义。如果您不执行任何操作并像这样images\/news_images\/Logos\/Logo.png 进行字符串存储,则将images/news_images 字符串替换为您存储的数据。
  • 根据您的描述,我认为您只想搜索images/news_images。 % 符号充当通配符,即使前后有其他字符,它也应该找到它。如果您不确定它是否会起作用,您甚至可以运行:SELECT * FROM images WHERE images LIKE '%images/news_images%' 这将允许您在运行更新查询之前查看受影响的行。
  • 我刚刚试了一下,得到了一个空的结果集
  • 如果我理解正确,您的代码应该在下面的文本中搜索术语images\/news_images 并将其替换为images\/news_images\/news_images 这是正确的吗? { "image_intro":"images\/news_images\/Logos\/Logo.png", "float_intro":"", "image_intro_alt":"", "image_intro_caption":"", "image_fulltext":"", "float_fulltext":"", "image_fulltext_alt":"", "image_fulltext_caption":"" }上面的内容是从数据库中复制过来的,所以这就是图片列每一行中存储的内容。
猜你喜欢
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
相关资源
最近更新 更多