如何将两个分隔列的值合并为一个?

时间:2016-01-08 15:09:46

标签: mysql sql-update

我有一张这样的表:

// mytable
+----+-------+-------------------+
| id | col1  |       col2        |
+----+-------+-------------------+
| 1  | one   | the first number  |
| 2  | two   | the second number |
| 3  | three | the third number  |
+----+-------+-------------------+

现在我想要这个:

// mytable
+----+-------+-------------------------+
| id | col1  |          col2           |
+----+-------+-------------------------+
| 1  | one   | one: the first number   |
| 2  | two   | two: the second number  |
| 3  | three | three: the third number |
+----+-------+-------------------------+

这是我的问题:

UPDATE mytable t1
  SET col2 = ( SELECT CONCAT(col1, ": ", col2) AS newcol
                 FROM mytable t2 WHERE t1.id = t2.id )

但是我的查询不起作用,它有这个错误:

  

#1093 - 您无法在FROM子句中为更新指定目标表't1'

我该如何解决?

1 个答案:

答案 0 :(得分:3)

你有没有尝试过:

 UPDATE mytable t1
 SET col2 = CONCAT(col1, ": ", col2);