I need to update a row in my mysql database, but i don't want to replace the data that is already stored in it. example:
select books from storedb where id='Rick';
result of the query: Books = "example1"
but i need to update that row and add more books.
update storedb set books='example2' where id='Rick';
but it replaces the current data, so i need to do it without replacing current data.
somethink like this: books='example1 -- example2";
so you need a string concatenation?
Try this:
update storedb set books=CONCAT(books,' -- ', 'example2') where id='Rick';
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 a table that contains directory paths and the data looks like this:
But when I run an update statement where I join another table to this one and update the to existing rows in the new table, the backslashes disappear like this:
This is the update statement, where
"MIJob.SourceFile"
has the proper text containing the backslashes, and
"MIJobFileLocation.Path_Folder"
is the column being updated and does not have backslashes in its data.
This is the update statement:
UPDATE MIJobFileLocation
INNER JOIN MIJob
ON MIJobFileLocation.MIJobFileLocationGUID = MIJob.MIJobFileLocationGUID_Source
SET
MIJobFileLocation.Path_Folder = MIJob.SourceFile
WHERE MIJob.SourceFile IS NOT NULL
This SQL will run in a stored procedure in MySQL. How can I preserve the backslashes?
I’ve been googling this for hours with no success.
Thank you.
Execute this line before your update statement. Seems like a hack solution, but it works.
SET SESSION sql_mode='NO_BACKSLASH_ESCAPES'
UPDATE sometable
set.....
Just wondering is it possible to add onto an exisiting entry in the database without having to do a query to look whats in there?
Ive done it with numbers before like
UPDATE table SET views = views +1
But is there a way to do it with a string?
Thanks
UPDATE table SET views = CONCAT(views,'some text to add on to it')
What you are looking for is CONCAT() function:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
UPDATE table SET field = CONCAT(field, "some str" ) WHERE id = 123;
I have in my db strings like www.domain.com and http://www.domain.com. I want to prepend to all entries the http:// but not affect other urls and as a result have this: http://http://www.domain.com
Can this be done with mysql only? I have used REPLACE(field,'www','http://www'), but this replaces also the http://www with http://http://www
Thanks in advance
EDIT
I forgot to mention that in the field there might be entries which don't contain www or http://www and therefore I don't want to alter or maybe there are entries like <p>domain</p> in which CONCAT() prepends the http:// before <p>
Try adding a WHERE clause to your update to only update fields that do not already have 'http://'. Test it out like this
SELECT CONCAT('http://', field) FROM foo WHERE LOCATE('http://', field)=0
and your UPDATE syntax would be:
UPDATE foo SET field=CONCAT('http://',field) WHERE LOCATE('http://', field)=0
I won't worry about performance as this seems like a one-off kind of script. That said, you can couple LEFT and CONCAT to achieve this:
UPDATE mytable
SET mycolumn = CONCAT('http://',mycolumn)
WHERE LEFT(mycolumn,7) <> 'http://'
Do note that I'm not taking CapItaliZation in to account. You may also want to consider sanitizing the information either before adding it to the database, or maybe make a trigger to do it for you.
Search and Replace Query - mysql replace
Here is the SQL query to replace string in your MySQL database table:
UPDATE table_name SET column_name = REPLACE(column_name,'original_string','replace_string')
Here is what I did to change the path URLs in all my previous posts.
UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`,'http://localhost/','https://sureshkamal1.wordpress.com/')
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);