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);
Related
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')
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);
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;
Here is a piece of mysql code
DELETE FROM tbl_node_details WHERE ParentId IN (1,2,3,4,5);
This works fine and all rows are deleted with the respective ids.
But when is do like this
SET #a='1,2,3,4,5'
DELETE FROM tbl_node_details WHERE ParentId IN (#a);
it doesn't work...y is it so??
You cannot use parameters with in statement. Try to loop or build string wo parameters
Cannot use parameters within query statements. Try using loops.
As #hkutluay stated this won't work.
Assuming you get your parameters from another table you can do :
DELETE FROM tbl_node_details WHERE ParentId IN (SELECT <id_col_name> FROM <other_table> WHERE <condition>);
I have a table, whose fields are
id, name, link
the link holds the name of the page like "link" = "index.php". Now I want to update this field
and add "page=" in front of "index.php". Using this method I would like to update every entry in my table.
My desired SQL syntax need to be something like this
UPDATE mytable set link= 'page=' + <existing value of link>;
Anyone know what to accomplish this?
If you're using MySQL you can try:
UPDATE mytable SET link = CONCAT("page:", link)
UPDATE mytable Set link = "page:" || link