SQL 连接两个表列并用结果更新一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8384688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:32:03  来源:igfitidea点击:

Concat two table columns and update one with result

sqlmergeconcatenation

提问by RYN

I have table that has two columns and I need to concatenate these two and update first column with result.
For example assume this is my table:

我的表有两列,我需要连接这两列并用结果更新第一列。
例如假设这是我的表:

+----+-------+-------+
| id | col1  | col2  |
+----+-------+-------+
|  1 | text1 | text2 |
+----+-------+-------+
|  2 | text3 | text4 |
+----+-------+-------+

after concatenation my table should be:

连接后我的表应该是:

+----+-------------+-------+
| id |    col1     | col2  |
+----+-------------+-------+
|  1 | text1.text2 | text2 |
+----+-------------+-------+
|  2 | text3.text4 | text4 |
+----+-------------+-------+

How can I do this using SQL?

如何使用 SQL 执行此操作?

回答by Marco

Try this (for MySQL)

试试这个(对于 MySQL)

UPDATE your_table
SET col1 = CONCAT_WS('.', col1, col2)

and this for MS-SQL

这对于 MS-SQL

UPDATE your_table
SET col1 =col1 || "." || col2

回答by dani herrera

Homeworks?

家庭作业?

I asume mysql:

我假设mysql:

update table t
set col1 = concat( col1, '.', col2)

回答by Sruit A.Suk

with MS SQL Server 2014 I used it like this

使用 MS SQL Server 2014 我是这样使用的

UPDATE CANDIDATES 
SET NEW_ADDRESS_EN = CANDI_HOME_NO + ', ' + 
CANDI_VILLAGE + ', ' + CANDI_ROAD + ' Road, ' + CANDI_PROVINCE