How to concatinate text on existing database entry? - mysql

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

Related

how to delete single record from list

I have a record in table column like this (1001,1002,1003,1004,1005)
and I want to delete "1003" from this list. Please help me.
For your own good, don't store data like this. This type of issue is not, by far, the biggest problem you will run into.
This being said, you can solve you issue by using:
UPDATE TABLE
SET COLUMN = REPLACE (COLUMN, ',1003,', ',')
WHERE ID = PK;
even this will work:
update tablename set column=(select
substr(column,1,instr(colname,',',2))||susbtr(column,instr(column,',',3),length(column)-
instr(column,',',3)) from tablename where id=value;

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 Add onto an exisiting entry in the database

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;

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);