Reputation: 13
I have a table as such:
ID Name SignUp Unique
1 James August2020 JamesAugust2020
2 Tom August2020 TomAugust2020
3 Dick September2020 DickSeptember2020
4 Larry June2020 LarryJune2020
5 Sam July2020 SamJuly2020
6 John July2020 JohnJuly2020
7 Frank March2020 FrankMarch2020
8 Jason August2020 JasonAugust2020
If the 'SignUp' column were to change I would like to run an update query that would concatenate and update the unique column to the new 'SignUp' date for each individual with the same 'SignUp' date.
See Example (If all 'SignUp' dates were to change from August2020 to April2020, I would like the following to happen)
ID Name SignUp Unique
1 James April2020 JamesApril2020
2 Tom April2020 TomApril2020
3 Dick September2020 DickSeptember2020
4 Larry June2020 LarryJune2020
5 Sam July2020 SamJuly2020
6 John July2020 JohnJuly2020
7 Frank March2020 FrankMarch2020
8 Jason April2020 JasonApril2020
How would I do this using an update statement in MySQL?
Upvotes: 0
Views: 66
Reputation: 4030
Table structure:
ID Name SignUp Unique
1 James August2020 JamesAugust2020
2 Tom August2020 TomAugust2020
3 Dick September2020 DickSeptember2020
4 Larry June2020 LarryJune2020
5 Sam July2020 SamJuly2020
6 John July2020 JohnJuly2020
7 Frank March2020 FrankMarch2020
8 Jason August2020 JasonAugust2020
If all 'SignUp' dates were to change from August2020 to April2020.
You can update using 2 methods:
Method 1:
UPDATE TABLE
SET SignUp='April2020',
UNIQUE = REPLACE(UNIQUE, 'August2020', 'APRIL2020')
WHERE SignUp='August2020';
You can check detailed documentation of REPLACE
Method 2:
Or you can try another way, concat Name
and Unique
column
UPDATE TABLE
SET SignUp = 'April2020',
`Unique` = CONCAT(Name, SignUp)
WHERE SignUp = 'August2020'
Upvotes: 0
Reputation: 1269563
If you want unique
to be the concatenation of the other two columns, then you should define it as a generated column:
alter tab t add column `unique` varchar(255) generated always as
( concat(name, signup) );
It is then generated when the table is queries and there is no need (or even possibility) of updating the column.
Upvotes: 1
Reputation: 164069
You can do it like this:
UPDATE tablename
SET SignUp = 'April2020',
`Unique` = CONCAT(Name, SignUp)
WHERE SignUp = 'August2020'
In this assignment:
`Unique` = CONCAT(Name, SignUp)
MySql uses the changed value of SignUp
.
See the demo.
Results:
| ID | Name | SignUp | Unique |
| --- | ----- | ------------- | ----------------- |
| 1 | James | April2020 | JamesApril2020 |
| 2 | Tom | April2020 | TomApril2020 |
| 3 | Dick | September2020 | DickSeptember2020 |
| 4 | Larry | June2020 | LarryJune2020 |
| 5 | Sam | July2020 | SamJuly2020 |
| 6 | John | July2020 | JohnJuly2020 |
| 7 | Frank | March2020 | FrankMarch2020 |
| 8 | Jason | April2020 | JasonApril2020 |
Upvotes: 0
Reputation: 65218
One option would be using REPLACE()
for Unique
column
UPDATE tab
SET `SignUp` = 'April2020',
`Unique` = REPLACE(`Unique`,'August2020', 'April2020')
WHERE `SignUp` = 'August2020'
during the update of SignUp
column
Upvotes: 0