MySQL update column based on another column data manipulation - mysql

I have a table looks like below:
id name value newColumn
============================
1 Joe 22 null
2 Derk 30 null
newColumn is a newly added column and I would like to update the data for each and every rows with a specific format: concat value and a specific string -CIF
End result should be something like below:
id name value newColumn
============================
1 Joe 22 22-CIF
2 Derk 30 30-CIF
May I know how to construct such update query in mySQL?

This is a fairly basic update:
update table t
set newColumn = concat(value, '-CIF');

Related

MySQL update without primary key

i'm looking to figure out how i can update a row like a LIMIT 2,2 on a select
there is a little example:
col1 | col2
5 10
5 10
5 10
I want to update the 2nd row like this:
col1 | col2
5 10
1 10
5 10
i know the row number so i want something like that:
UPDATE table
SET col1 = 1
WHERE col1 = 5
LIMIT 2, 1
we cant use limit but i know this is achievable, HeidiSQL can do it and i'm trying to figure out how they are doing
thanks
If you need to update particular rows, you need a column or columns that can be used as primary key.
SQL works on sets of rows and you can only update rows that can be identified as belonging to the set.
For example, you can
UPDATE Customers SET Preferred=True WHERE TotalSales > 1000
which will set the "Preferred" flag for any customers that have sales over 1000. This might be one customer or a million or none.
The only way to do the single row update you asked about is to have some way to identify the row. In many database servers you can configure an IDENTITY or SEQUENCE column that will auto-assign each row a unique ID.
You can add an ID column with the IDENTITY property set, which would get you:
ID | col1 | col2
1 5 10
2 5 10
3 5 10
So updating that particular row would be:
UPDATE table SET col1 = 1 WHERE ID = 2

How to copy one column's value to another before updating

I am runnning a Postgres and Mysql server and I have table:
id | name | age | old_age
1 abc 20
I want to update column age with 21 with storing old value 20 into old_age column.
I can fetch the row and read the value of age and then update but that will require running 2 queries. 1 select and 1 update. Is there any way we can update in 1 query? I just want to copy age to old_age.
Just do
UPDATE TableName SET old_age=age, age=21 WHERE

Modify All varchar elements in a MySQL column

In a MySQL database,
I have a table License with a few example rows as presented below:
ID | Key | Location
1 25 C:/Public/lics/1885-0001.lic
3 21 C:/Public/lics/1885-0006.lic
There are many such rows, which I would like to modify as given below:
ID | Key | Location
1 25 C:/Licenses/1885-0001.lic
3 21 C:/Licenses/1885-0006.lic
One of the columns from all the rows get modified. How do I update the table to make this change across all rows.
Judging from the docs I posted in my comment, I think you should do something like this:
UPDATE License SET Location = REPLACE(Location, 'C:/Public/lics', 'C:/Licenses');
UPDATE License
SET Value = REPLACE(Location, 'Public/lics', 'Licenses')

MySQL update values using set

I want to update the value in my handicap column with the value in handicapChange column
My Table looks like:
Name | Handicap | Points | HandicapChange
Joe Bloggs | 21.00 | 39 | 20.50
What I want to do is update the Handicap column with the value(20.50) in HandicapChange column.
I was thinking something like:
update table comp SET Handicap = HandicapChange
Thanks
You should use UPDATE comp SET Handicap = HandicapChange WHERE Name = 'Joe Bloggs', otherwise you'll set these values for every record currently present in the table.
UPDATE table SET Handicap = HandicapChange will work, but it will assign the value of HandicapChange to Handicap to EVERY row in the table. If this isn't what you want, make sure you add a WHERE clause targeting only those rows that you actually want to change.
If you want to update the entire table than use that query. If you want to narrow down your set of records to update you'll need to specify parameter using WHERE.
update table comp SET Handicap = HandicapChange WHERE Name = 'Joe Bloggs'
See MySQL for more info.

How to delete specific values from table

I have the following table structure:
table "sample"
objectID | objectValues
1 | 5,6,7
2 | 6,7,5,8
3 | 5
4 | 7,8,9,5,6
5 | 10,11
6 | 5
So, I want to delete all ",5" or "5," values from "objectValues" and delete all columns which theirs "objectValues" value is only "5". How can I do this?
You can try a tricky method using four queries to make this change.
first query for delete all rows when object value is 5
DELETE FROM sample WHERE objectValues = 5;
second for update rows when is a value is in all places except first and last
UPDATE sample
SET objectValues = REPLACE(objectValues , ',5,', ',');
third when the value 5 is on the first place
UPDATE sample
SET objectValues = LEFT(objectValues, LENGTH(objectValues)-2)
WHERE objectValues LIKE '5,%';
and last query when the value 5 is on the last place
UPDATE sample
SET objectValues = RIGHT(objectValues, LENGTH(objectValues)-2)
WHERE objectValues LIKE '%,5';
Try this
delete from table where find_in_set(5,objectValues)>0;