Solved

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

Posted on 2014-10-23
3
338 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Importing and exporting data Magento 1.x ? 4 87
insert row field data graphically 4 24
unable to insert record into a table 2 31
UPDATE query not working in mysqli php 8 52
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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