How to change photopath in mysql? - mysql

I have total 5000 data in mysql how is it possible to change this photopath ?
my current data:
(photos/date/datefullname.jpg)
photos/20151117/20151117samplename.jpg
to:
(photos/applicants/date/datefullname.jpg)
photos/applicants/20151117/20151117samplename.jpg
adding applicants in photopath.

Solved using this:
UPDATE sample
SET Value = REPLACE(Value, 'photos/', 'applicants/')

you can use something like that
UPDATE table SET fieldname=REPLACE(fieldname,'photos/date','photos/applicants/date')
but you should not store the url in the database as you can change the path without edit in the database
you can see this link for more explanation
https://www.quora.com/What-are-the-ways-to-insert-an-image-path-into-the-MySQL-database-and-save-it-in-the-folder-and-display-the-same-image-in-a-web-page-via-PHP/answer/Gajus-Kuizinas

Related

MySql, Copy one column into another and add some text

I have a column which has an Article-ID and one empty with the picture filename. Example: The article with the ID 34.67 should get the filename 34.67.jpg and so on. There are about 20'000 articles.
What would be the best way to do this?
Depending on your table structure or SQL dialect - it will be something like this:
UPDATE Tablename SET filename=concat(articleid, '.jpg')
OK, Thank you very much. I ended up using following code. This worked like a charm.
UPDATE oxarticles SET OXPIC1=LOWER(concat(OXARTNUM, '.jpg')) WHERE OXPIC1 = ' '

Mysql - change data for all users

I'm looking for solution to change data for all user in one column. Now I have column "user_personal_image" and there are encoded array with info:
{"original":"photo_uploads\/original_2O52S6nhQrCSv3RZStlbY.jpg","thumb_index":"photo_uploads\/thumb_index_2O52S6nhQrCSv3RZStlbY.jpg"}
and I need to change thumb_index to thumb for all users using only SQL query in client, is this even possible?
P.S. all data is different in all columns, there is only same keys "original" and "thumb_index"
Assuming you want to change both the array key and the path This should do it:
UPDATE your_table
SET user_personal_image = REPLACE(user_personal_image, 'thumb_index', 'thumb')
This would change the string to:
{"original":"photo_uploads/original_2O52S6nhQrCSv3RZStlbY.jpg","thumb":"photo_uploads/thumb_2O52S6nhQrCSv3RZStlbY.jpg"}
If you only want to change the array key use:
REPLACE(user_personal_image, '"thumb_index"', '"thumb"')
or if it is the path use:
REPLACE(user_personal_image, '/thumb_index', '/thumb')
Sample SQL Fiddle
Try this query...
UPDATE yourTable
SET user_personal_image = REPLACE(user_personal_image, 'thumb_index', 'thumb')

mysql add http:// to all records in website column

I'm almost done transferring/reconstructing a substantial mysql db for new application. Column 'website' shows 'www.example.com'. Because the new application reads it as a hyperlink, I need the column to read 'http://www.example.com'. Is there a way to add the 'http://' in the beginning of each record for that column? Thanks in advance!
You can use the CONCAT function to do that:
UPDATE tbl SET website=CONCAT('http://', website);
If you want to get cleverer and only update columns which don't already have http:// prepended, try
UPDATE tbl SET website=CONCAT('http://', website)
WHERE website NOT LIKE 'http://%';
Update
To prevent update to columns that have no site in them currently use this
UPDATE tbl SET website=CONCAT('http://', website)
WHERE website NOT LIKE 'http://%' AND website<>'';
You can use the concat command. Something like
SELECT CONCAT('http://', website) FROM table
Use concat to generate the new column:
UPDATE table1 SET website = CONCAT("http://", website);

MySQL REPLACE variable string

I want to use MySQL query to change a link .
the link is like this :
http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere
if I know the add_id number this is easy :
UPDATE table SET name = REPLACE(name, '&add_id=548124', '')
The problem is I have to change 5000 lines and I don't know the add_id number ... so what would be a correct mysql replace() code to remove &add_id=somenumber ??
USE This....
UPDATE table
SET name = CONCAT(SUBSTRING(name , 1,
INSTR(name ,'&add_id') - 1),SUBSTRING(name ,
INSTR(name , '&more'),
LENGTH(name ) - INSTR(name , '&add_id')))
Either you can do it via UDF - SO Answer or you can simply write PHP code which will replace value & update column again in table.
I would create a stored procedure that uses a cursor to iterate over each row that needs updating.
In the procedure I would find the link to replace and then replace them, one by one.
I've made a sqlfiddle to show how you can get the part to replace inside an select.
I think this approach is clean and easy to read but it's possible to write this a update (that will most likely be hard to read).
first to see that it works :
SELECT 'http://website.com/click.php?ad_client=blablabla&add_id=548124&more=stuffhere' INTO #link;
SELECT
#link as full_link,
SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)) as remove_part,
REPLACE(#link,SUBSTR(#link,LOCATE('&',#link),LOCATE('&',#link,LOCATE('&',#link)+1)-LOCATE('&',#link)),'') as final_link
And now for your UPDATE:
UPDATE table SET name = REPLACE(name,SUBSTR(name,LOCATE('&',name),LOCATE('&',name,LOCATE('&',name)+1)-LOCATE('&',name)),'')
try this with REPLACE
UPDATE Table1
SET name = REPLACE(if(name like '%add_id=%','' , name ),
'&add_id=' , '' )
DEMO HERE

add the same thing to a table column by sql command

there is a table named test. in it. there are some columns as this:
001.jpg
...
999.jpg
now i want to use a sql command to add a url before them. as this http://www.example.com/xxx/001.jpg.....is there a way to get this? thank you.
There is two way to accomplish this task. It depends up to you that what you want?
If you want to add a url in the database permanently then you have to use update query with no any where condition, Although if you want to only show your field with this added url you have to use select query.
Please find below the examples for both:
Suppose that your table column name is imageName then UPDATE query will be
UPDATE test SET imageName = CONCAT("http://www.example.com/xxx/", imageName);
And the SELECT query will be
SELECT CONCAT("http://www.example.com/xxx/", imageName) FROM test;
Supposing that your field is called url, a simple UPDATE query will do:
UPDATE test SET url = CONCAT("http://", url);