CREATE TABLE `photos` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`photo` varchar(255) NOT NULL,
`hotel_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23061 DEFAULT CHARSET=utf8
So, i have table ‘photos.’
Now i want ALTER this table and add column ‘type’.
ALTER TABLE photos
ADD COLUMN type varchar(50) default 'jpg'
But there is one problem:
column ‘photo’ contains path – ‘/foo/bar.jpg’ or ‘/foo/bar.png’ and i want to update ‘type’ column according to ‘photo’ column content. So type must be ‘jpg’ if photo ends with ‘jpg’ or type must be ‘png’ if photo ends with ‘png’.
I have no idea how to write such script…
You could use a
casestatement for that:Or if the type is always the last 3 characters: