【问题标题】:How to match on string length to get the data?如何匹配字符串长度以获取数据?
【发布时间】:2021-01-28 07:32:45
【问题描述】:

我有 emp1 和 emp2 表。我正在尝试通过计算 emp_id 将几个值从 emp2 更新为 emp1。但是 emp1 有正确的 emp_id ,因为 emp2 末尾有其他字符。如何根据字符长度匹配 emp_ids((例如:如果 emp1 中有 3 个字符,则匹配 emp2 中的前 3 个字符,emp1 中的 4 个字符则匹配前 4 个字符等)

**TABLE: EMP1                                  TABLE2: EMP2**
    
EMP_ID  DEPT                                  EMP_ID      DEPT
                                            
AAA      Accounts                            AAA12        Accounts
BBBBB    HR                                  BBBBBrcp      HR 

-提前致谢。

【问题讨论】:

  • 如果附加字符带有相关信息,则将它们放在额外​​的列中。否则丢弃它们...

标签: string postgresql sql-update inner-join sql-like


【解决方案1】:

您可以加入like。不清楚你想在updateemp2 中做什么,但逻辑是:

update emp2 e2
set col1 = e1.col1, col2 = e1.col2  -- columns you want to update go here
from emp1 e1
where e2.emp_id like e1.emp_id || '%'

注意emp2 中的每个员工只能匹配emp1 中的一行,否则可能会发生意外情况。

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 2017-06-12
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多