[英]MySQL - Update Column by Concatenating Field At Each Row
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)请参阅示例(如果所有“注册”日期都从 2020 年 8 月更改为 2020 年 4 月,我希望发生以下情况)
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?我将如何使用 MySQL 中的更新语句来执行此操作?
If you want unique
to be the concatenation of the other two columns, then you should define it as a generated column:如果您希望
unique
是其他两列的串联,那么您应该将其定义为生成的列:
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.然后在表被查询并且不需要(甚至不可能)更新列时生成它。
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
. MySql 使用
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 |
One option would be using REPLACE()
for Unique
column一种选择是将
REPLACE()
用于Unique
列
UPDATE tab
SET `SignUp` = 'April2020',
`Unique` = REPLACE(`Unique`,'August2020', 'April2020')
WHERE `SignUp` = 'August2020'
during the update of SignUp
column SignUp
栏更新期间
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.如果所有“注册”日期都从 2020 年 8 月更改为 2020 年 4 月。
You can update using 2 methods:您可以使用 2 种方法进行更新:
Method 1:方法一:
UPDATE TABLE
SET SignUp='April2020',
UNIQUE = REPLACE(UNIQUE, 'August2020', 'APRIL2020')
WHERE SignUp='August2020';
You can check detailed documentation of REPLACE您可以查看REPLACE的详细文档
Method 2:方法二:
Or you can try another way, concat Name
and Unique
column或者你可以尝试另一种方式, 连接
Name
和Unique
列
UPDATE TABLE
SET SignUp = 'April2020',
`Unique` = CONCAT(Name, SignUp)
WHERE SignUp = 'August2020'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.