Solved

Need to add a forward slash "/" to all image fields in MySQL table?

Posted on 2014-10-23
3
339 Views
Last Modified: 2014-10-23
Hi,
I need to add a forward slash "/" to all image fields in MySQL table only where they don't already exits.

Is there an easy way to do this?

Thanks in advance for your help.

CREATE TABLE IF NOT EXISTS `items_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `image1` varchar(60) DEFAULT '/uploads/nopic.jpg',
  `image2` varchar(60) DEFAULT '/uploads/nopic.jpg',
  `image3` varchar(60) DEFAULT '/uploads/nopic.jpg',
  `image4` varchar(60) DEFAULT '/uploads/nopic.jpg',
  `image5` varchar(250) DEFAULT '/uploads/nopic.jpg',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1151 ;



INSERT INTO `items_test` (`id`, `image1`, `image2`, `image3`, `image4`, `image5`) VALUES
(802, 'uploads/item802_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg'),
(802, '/uploads/products_good.jpg', '/uploads/products_good2.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg'),
(891, 'uploads/new_image_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg');

Open in new window

0
Comment
Question by:sabecs
  • 2
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40400856
UPDATE items_test
SET
image1=CONCAT('/',image1)
WHERE
!(SUBSTR(image1, 1, 1)='/')

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40400859
Forget that, didn't see multiple fields

UPDATE
  items_test
SET
  image1 = IF (!(SUBSTR(image1, 1, 1)='/'), CONCAT('/',image1),image1),
  image2 = IF (!(SUBSTR(image2, 1, 1)='/'), CONCAT('/',image2),image2),
  image3 = IF (!(SUBSTR(image3, 1, 1)='/'), CONCAT('/',image3),image3),
  image4 = IF (!(SUBSTR(image4, 1, 1)='/'), CONCAT('/',image4),image4),
  image5 = IF (!(SUBSTR(image5, 1, 1)='/'), CONCAT('/',image5),image5)

Open in new window

0
 

Author Closing Comment

by:sabecs
ID: 40400896
Thanks Gary, very much appreciated.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question