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')
Related
I want to add "/invent" at the beginning of the file path in the invImage and invThumbnail columns.
This is what I have in mind,
SELECT FROM inventory
SELECT CONCAT("/invent")
AS invImage, invThumbnail;
Since it is not easy to undo mistakes in SQL, a confirmation of my potential solution will be helpful.
Your current version will overwrite the value in the invImage column, but your stated goal is to
a) prepend the string to the existing value, and
b) do this in two columns, not just one
WHERE invImage = "/images"; also won't match any of the rows, because none of them contain exactly that value. I'll assume you want to update all rows whose invImage value starts with "/images".
Therefore, try this:
UPDATE
inventory
SET
invImage = CONCAT("/invent", invImage),
invThumbnail = CONCAT("/invent", invThumbnail),
WHERE
invImage LIKE "/images%";
My suggestion: add temporarily two columns to the table: tmp_invImage and tmp_invThumbnail.
Then run the query:
UPDATE table SET
tmp_invImage = CONCAT("/invent",invImage),
tmp_invThumbnail = CONCAT("/invent",invThumbnail)
After the update, look if the values of tmp_invImage and tmp_invThumbnail are correct.
Then update the original columns:
UPDATE table SET
invImage = tmp_invImage),
invThumbnail = tmp_invThumbnail
and delete the tmp_ columns.
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
So I have a bunch of users in a column that get refreshed as:
Bill#test.comXYZ
Tom#test.comXYZ
John#test.comXYZ
We refresh the database each week and I need to update these appropriate emails to:
Bill#domain.com
Tom#domain.com
John#domain.com
I figured I can use concat to do the latter, but I am stuck on the former issue. Is there a way to split the values (like split Bill#test.comXYZ into Bill - #test.comXYZ and then remove the #TEXT values?).
Anyways, any help will be much appreciated.
You can use the mySQL replace function, i.e.
UPDATE mytable
set myfield = replace (myfield, '#test.comXYZ', 'domain.com')
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace
I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.
If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.
You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html
No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.
Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html
I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.
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);