KouShuiYu
V2EX  ›  问与答

怎么让数据库中 int array 类型的字段值自增 100? 比如: {1,2,3} => {101, 102, 103}

  •  
  •   KouShuiYu · Dec 17, 2020 · 995 views
    This topic created in 1979 days ago, the information mentioned may be changed or developed.
    Supplement 1  ·  Dec 17, 2020

    用了种比较丑都方式修改的,幸苦数组不太长

    -- select array_upper(adv_ids,1) from deeplinks
    update deeplinks set adv_ids[1] = adv_ids[1] + 100 where array_upper(adv_ids,1) >= 1;
    update deeplinks set adv_ids[2] = adv_ids[2] + 100 where array_upper(adv_ids,1) >= 2;
    update deeplinks set adv_ids[3] = adv_ids[3] + 100 where array_upper(adv_ids,1) >= 3;
    update deeplinks set adv_ids[4] = adv_ids[4] + 100 where array_upper(adv_ids,1) >= 4;
    update deeplinks set adv_ids[5] = adv_ids[5] + 100 where array_upper(adv_ids,1) >= 5;
    update deeplinks set adv_ids[6] = adv_ids[6] + 100 where array_upper(adv_ids,1) >= 6;
    
    2 replies    2020-12-17 13:34:59 +08:00
    lekai63
        1
    lekai63  
       Dec 17, 2020
    我记得 postgres 中是可以对字段自定义步长的
    KouShuiYu
        2
    KouShuiYu  
    OP
       Dec 17, 2020
    @lekai63 你理解错了修改现有的数组里的元素值,+=100
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1126 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 23:01 · PVG 07:01 · LAX 16:01 · JFK 19:01
    ♥ Do have faith in what you're doing.