Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove Database Duplicates with Similar Name
    primarykey
    data
    text
    <p>I have a table in a database that stores the name of the image files where they can be retrieved from on the server. </p> <p>The table description is as follows:</p> <pre><code>+------------+-----------------+------+-----+-------------------+-------+ | 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 | | +------------+-----------------+------+-----+-------------------+-------+ </code></pre> <p>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. </p> <p>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.</p> <p>Out of all the fields in the table. The only fields in the table that tells the old and new apart are the <code>id</code>, <code>mod_date</code> (which are both automatically generated) and the <code>image_name</code>. </p> <p>The image name both share the same string but is pre-pended with different paths information.</p> <p>Example:</p> <p>old image name: <code>XXX_02420624_20100308-00231_A.png</code> </p> <p>new image name: <code>YYY_02420624_20100308-00231_A.png</code> </p> <p>Notice that <code>02420624_20100308-00231_A.png</code> are being shared by both old and new with different pre-pends. </p> <p>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?</p> <p>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?</p> <p><strong>EDIT:</strong> The newly added image names has the format of </p> <blockquote> <p>yyy_userid_albumid_AAA.png</p> </blockquote> <p>where <code>yyy</code> is the pre-pend identical for all new images, <code>userid</code> and <code>albumid</code> pretty much self-explanatory, and <code>AAA</code> is the unique image name.</p> <p>Since I know the <code>xxx</code> and <code>yyy</code> 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 <code>yyy</code> and then change the <code>xxx</code> to <code>yyy</code> 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 '<code>image_name</code>' for the pre-pend until it hit the 8 digit <code>userid</code> (that starts with 0)?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload