【问题标题】:Putting splitted strings into each row将拆分的字符串放入每一行
【发布时间】:2019-09-06 11:35:01
【问题描述】:

我的问题是这样的:

假设有一个表(People),我们有一个名为 name 的列,有 5 行,所以在我们的例子中:

Name:
Enrique West
Damien Braun
Ellie Osborne
Cierra Vega
Alden Cantrell

然后我们有另一列,称为姓氏。此栏为空。 现在我需要 sql 代码将所有这些人的姓氏放在姓氏列中。 那么我们应该有:

Surname:
West
Braun
Osborne
Vega
Cantrell

我试过这个:

Update People
set Surname =
(SELECT SUBSTRING(name, 23, 20)
from People)
where condition

我目前收到此错误:

Subquery returned more than 1 value. This is not permitted when the 
subquery follows =, !=, <, <= , >, >= or when the subquery is used as an 
expression.

重要的是每一行,例如 Enrique West 所在的行,只包含他的姓氏。

我应该在代码中添加或更改什么?

非常感谢!

【问题讨论】:

  • 将 Malcolm McDowell、Conan O'Brien、Carl van der Walt 和 Jonathan Rhys-Davies 也添加到样本数据中。

标签: sql split


【解决方案1】:

不要使用子查询!只是:

Update People
    set Surname = SUBSTRING(name, 23, 20)
    where condition;

【讨论】:

  • 谢谢!这就是解决方案!
猜你喜欢
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多