Reputation: 3571
I have a table in a database that stores the name of the image files where they can be retrieved from on the server.
The table description is as follows:
+------------+-----------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-----------------+------+-----+-------------------+-------+
| id | varchar(50) | NO | PRI | NULL | |
| userid | varchar(8) | NO | | NULL | |
| albumid | varchar(25) | NO | | NULL | |
| image_name | varchar(256) | NO | | NULL | |
| status | int(1) unsigned | NO | | NULL | |
| comments | varchar(4000) | YES | | NULL | |
| mod_date | timestamp | NO | | CURRENT_TIMESTAMP | |
+------------+-----------------+------+-----+-------------------+-------+
I have a separate PHP script that scans the image folders for new files and adds them to the database. However, someone who was trying to update the database deleted all the folders (for every single userid and albumid...) along with all the old images and recreated all folder with a set of new images with new names.
This left the website displaying broken picture links of the old set of images but with all the status and comments made on there, while the new set of images displays correctly but with no status and comment information stored.
Out of all the fields in the table. The only fields in the table that tells the old and new apart are the id
, mod_date
(which are both automatically generated) and the image_name
.
The image name both share the same string but is pre-pended with different paths information.
Example:
old image name: XXX_02420624_20100308-00231_A.png
new image name: YYY_02420624_20100308-00231_A.png
Notice that 02420624_20100308-00231_A.png
are being shared by both old and new with different pre-pends.
My question is.. if not all the albums are being updated (i.e. even though all the folders for all users and all albums are deleted and then added back, not all of them have new file names), what is the most efficient way of removing these duplicate data and still keep the comments and status assigned for each image?
I figure I can delete the new rows of images and rename the old image name into the new one so it points correctly to the right image and still retains the status/comments. However, how do I write a general script that search for the new images that are automatically stored in the DB?
EDIT: The newly added image names has the format of
yyy_userid_albumid_AAA.png
where yyy
is the pre-pend identical for all new images, userid
and albumid
pretty much self-explanatory, and AAA
is the unique image name.
Since I know the xxx
and yyy
are both constants, I can write a script in php that automatically looks through the database and delete the entries that has image_name beginning with yyy
and then change the xxx
to yyy
so that the image points to the right path and still retains the status and comment. My question is: how can you write a script that only search within the string of 'image_name
' for the pre-pend until it hit the 8 digit userid
(that starts with 0)?
Upvotes: 0
Views: 198
Reputation: 1329
Seems like you need to link the new names to the old images' records.
The SQL below should update the image names of all the old 'xxx' images with new image names. You will then just have to delete the new records which were added (ones prefixed with yyy)
(untested)
update the_table t
set t.image_name =
(select COALESCE(t2.image_name,t.image_name)
from the_table t2
where = t2.image_name = 'yyy' || substring(t.image_name,<size of xxx>))
Upvotes: 1