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

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

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

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 column based on another column data manipulation

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

MySQL calculate the difference of two columns into a Total column

Name |InitialScore | CurrentScore | Total
-----------------------------------------------
Bart | 145 | 95 | -50
Homer | 230 | 260 | 30
Lisa | 111 | 179 | 68
I have a table with the values above, what im trying to do is have the Total column find the difference between the initical score and the current Score from the table named mods.
i have tried multiple variations of this:
ALTER TABLE mods
ADD Total AS tphs + won PERSISTED
but i keep getting a "#1064 - You have an error in your SQL syntax;" My current MySQL is 5.1.70. Is there a way to accomplish a persisted column labeled Total so when i make changes to the CurrentScore column it automatically updates the total?
You want to use triggers. A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. This event can be an insert, update, or delete.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
In this case, you want to setup a trigger before the insert.
CREATE TRIGGER insert_total BEFORE INSERT ON mods
FOR EACH ROW SET Total = NEW.InitialScore + NEW.CurrentScore;
This will add InitialScore and TotalScore together and put it in the Total record. FYI: This trigger will work for both INSERT and LOAD DATA.
update MYTABLE set Total = CurrentScore - InitialScore where 1=1
It will update all the rows and if you want to update some specific rows than update the where clause accordingly.
But if you want it to update automatically then use Triggers .

Bulk update column in mysql

I have a table where there is a column named "backfill_target" and its value is 1 but I need to change this to 20 for all 300 rows, how would I do this?
EDIT:
Here is a simplified version of my table
ID|name |backfill_target|first_record|second_record
0 | test| 1 |52 |54
try,
UPDATE tableName
SET backfill_target = 20
WHERE backfill_target = 1
Assuming you want to update every row.
UPDATE table SET backfill_target = 20