Update column with value from another column - mysql

In a SQL table I have two columns: the first contain a path and the second contains a value.
colunm1
/path/
colunm2
12345
I need to update the first column with the value that exists in the second column. to get this result :
colunm1
/path/12456/
I tried this, but not working
update tablename p
set p.colunm1 = "/path/'colunm2'/"

You have the right idea, but the SQL you shared uses column2 as a string literal. You could use the concat to concatenate the two columns:
UPDATE tablename
SET column1 = CONCAT(column1, column2)

You have to use CONCAT
update tablename p
set p.colunm1 = CONCAT("/path/",`colunm2`,"/");

Related

subtract the contents of one column from another columns in each row

I have a big MySQL table and I want to change the Description column.
I want to delete the beginning of the every cell in each row that matches the value in the Name column, while the rest of the cell remains the same.
Please help.
REPLACE is your friend.
use this to see the changes:
SELECT TRIM(REPLACE(Deescription,`name`,'')) as newField from youtTableName;
update your table
UPDATE youtTableName
SET `Deescription` = TRIM(REPLACE(Deescription,`name`,''))
;
You could use update whete the locate for the name in desciption is 1 (at the begin of the description)
update my_table
set description = substr( description, length(name)+1)
where locate(description, name ) = 1

Update SET with variable column names, variable values in variable rows PDO

I'm trying to update a certain column of certain row WHERE id is certain value. The thing is, the number/names of columns are variable, and so are their respective ids.
For example:
UPDATE table SET column1="hello" WHERE id = 5
UPDATE table SET column2="cucumber" WHERE id = 6
How can I do a single mysql query in PDO to do this?
First thing I tried is...
UPDATE table SET column1="hello", column4="bye" WHERE id IN(5, 6)
But that query will update BOTH of those columns in rows where it finds BOTH of those ids, and that's not what I'm looking for. Is it only possible to do this query by query?
Keep in mind that the argument after SET is variable, so the columns to be updated, their values and their respective ids are also variable.
A solution where you can just purely bind values would be great, but if I have to build the query string with escaped variables, then that's OK too.
Thank you.
You can do this
UPDATE table t1 JOIN table t2
ON t1.id= 5 AND t2.id= 6
SET t1.column1= 'hello',
t2.column2 = 'cucumber';
Or if you want to do this on a single column
UPDATE table
SET column2 = CASE id
WHEN 5 THEN 'hello'
WHEN 6 THEN ''
END
WHERE id IN(5, 6);

Split string in Mysql by colon and update a table record

I have created a table (Table A), which has a column (Column A) which stores values like this
Example:
ASUNMI:GI:PI:INP:EDM:20141001:NO34W:DERERTBYDAY14:NSW
ASUNMI:GI:PI:HME:EDM:20140929:EO23M:WIERTNACAR:VICETC
I need to split this string and place the data in different columns.
Example:
Column2=ASUNMI
Column3=GI
Column4=PI
Column5=INP
Column6=EDM
I need to split the above string based on colons(:).the no of colons in each field could differ hence I cannot use the
substring_index(çolumn,':',-2) property
I need to then use this to update a table
this is a good link please check this out
if you want to update TableA you can write
UPDATE `TableA` SET `columName` = (SELECT SPLIT_STR(columnName, ':',1) as ColumnName from tableName)
or
UPDATE `TableA` SET `columName` = (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(columName,':',1),':',-1) AS columName FROM tableName)

Adding text to each column of MYSQL Database

I have a database table in MYSQL with around 1000 rows. In the table I have a column called 'overview'. In each row, this column has some value and at the end of that value I have a specific line (text) starting with: 'Source...'
Now what I want is, I want to remove this line from each column and replace it with some other text content.
I believe it can be accomplished with some smart query.
You can simply use REPLACE in your query like this
UPDATE your_table SET col_name = REPLACE(col_name , ‘Source...’, ‘new_val’)
WHERE col_name LIKE '%Source...';
Check Out the SQLFIDDLE.
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string).
The syntax of REPLACE is:
REPLACE (text_string, from_string, to_string)
In your case, you can do this way:
UPDATE `tableName` SET `column` = REPLACE(column , 'Source...', 'Replaced Value')
Use Replace
update TBL
set overview = Replace(picture, 'Source..', 'replacement')
keep a backup of the table before anything.Or you can do it on a copy.
you can do this by following:
update table_name set col_name = replace(column_name , ‘Source...’, ‘Replaced String...’);

How to prepend a string to a column value in MySQL?

I need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value.
For example, if the existing value is "try" it should become "testtry".
You can use the CONCAT function to do that:
UPDATE tbl SET col=CONCAT('test',col);
If you want to get cleverer and only update columns which don't already have test prepended, try
UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';
UPDATE tablename SET fieldname = CONCAT("test", fieldname) [WHERE ...]
Many string update functions in MySQL seems to be working like this:
If one argument is null, then concatenation or other functions return null too.
So, to update a field with null value, first set it to a non-null value, such as ''
For example:
update table set field='' where field is null;
update table set field=concat(field,' append');
That's a simple one
UPDATE YourTable SET YourColumn = CONCAT('prependedString', YourColumn);
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column1) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2, 'newtring2') where 1
We can concat same column or also other column of the table.