MySQL 选择为 var 并在更新中使用

mysql select as var and use in an update

提问人:mike 提问时间:1/31/2022 更新时间:1/31/2022 访问量:15

问:

我有一个包含这些字段的数据库:

picture_id  - (varchar) generally the actual filename of the picture (ie. myfile001.jpg)
event - (varchar) string describing the event (ie. Bob's Wedding 2005)
date_of_picture - (varchar) string representing a date (ie. 2004.01.45, Jan 2004, Summer 1969, etc)
filename - (text) - the full path to the picture, including the filename (ie. /pics/bob/2005/wedding)

我处于需要更改某些文件名条目的位置。

有没有办法查询事件并使用生成的picture_id来更新文件名?

像这样:

select picture_id as picid from photos where 
   event='Bob's Wedding 2005' update set filename='/my/new/path/here/$picid'

任何给定事件都可能有 100 个picture_id。

我不是DBA,这只是我个人使用的简单数据库,所以如果我的问题不清楚或我的数据库结构完全业余,请原谅我。

mysql 选择 sql-update

评论


答:

1赞 Tim Biegeleisen 1/31/2022 #1

将您的选择转换为适当的更新,我们可以尝试:

UPDATE photos
SET filename = CONCAT('/my/new/path/here/', picture_id)
WHERE event = 'Bob''s Wedding 2005';