KouShuiYu
V2EX  ›  问与答

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

  •  
  •   KouShuiYu · Dec 17, 2020 · 1322 views
    This topic created in 2160 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
    lekai63
    Dec 17, 2020
    1
    我记得 postgres 中是可以对字段自定义步长的
    KouShuiYu
    KouShuiYu
    OP
    Dec 17, 2020
    2
    @lekai63 你理解错了修改现有的数组里的元素值,+=100
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2637 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 04:29 · PVG 12:29 · LAX 20:29 · JFK 23:29
    ♥ Do have faith in what you're doing.