A simple question for MySQL-pro. I've got a table that has a field with the filename in it (just the filename, no extra text). I need to change all file extension from ".png" to ".jpg", I know there's a way to do it with just query and no scripting in the programming language like PHP or Java.
Just in case, dropping "show create table" output:
CREATE TABLE `photos` (
`id` bigint(20) NOT NULL,
`owner_id` int(11) DEFAULT NULL,
`photo_name` varchar(255) DEFAULT NULL,
`comment` text,
`normal_file_name` varchar(255) DEFAULT NULL,
`thumb_file_name` varchar(255) DEFAULT NULL,
`full_file_name` varchar(255) DEFAULT NULL,
`photo_order` int(11) DEFAULT NULL,
`gallery_file_name` varchar(255) DEFAULT NULL,
`photo_type` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_photos_OWNER_ID` (`owner_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
The normal_file_name, thumb_file_name, gallery_file_name and full_file_name are fields with file paths.
Thanks for help in advance!
// Juriy
The syntax to modify a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name MODIFY column_name column_definition [ FIRST | AFTER column_name ]; table_name. The name of the table to modify.
To change a column's definition, use MODIFY or CHANGE clause along with the ALTER command. mysql> ALTER TABLE testalter_tbl MODIFY c CHAR(10); With CHANGE, the syntax is a bit different. After the CHANGE keyword, you name the column you want to change, then specify the new definition, which includes the new name.
In order to get file extension of file as a result of SQL query, you can use SUBSTRING_INDEX(). Insert some records in the table using insert command. Display all records from the table using select statement.
Use the REPLACE function in an UPDATE statement:
UPDATE PHOTOS
SET normal_file_name = REPLACE(normal_file_name, '.png', '.jpg'),
thumb_file_name = REPLACE(thumb_file_name, '.png', '.jpg'),
gallery_file_name = REPLACE(gallery_file_name, '.png', '.jpg'),
full_file_name = REPLACE(full_file_name, '.png', '.jpg')
If there's no match, the replacement won't happen.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With