【问题标题】:KDB Query to update a column with listKDB 查询以使用列表更新列
【发布时间】:2022-01-02 18:34:24
【问题描述】:

我需要用一个只有一个符号的列表来更新一列。下面一个不工作。请注意,city 是一个包含符号列表的列。

update city: enlist `Lodnon from `user where id in (1,2,3);

【问题讨论】:

    标签: kdb


    【解决方案1】:

    您当前的查询非常接近。您想提供一个原子(即 `london )而不是一个列表:

    update city:`london from user where id in 1 2 3

    您建议的查询将因长度错误而失败(您提供一个包含 1 个元素的列表来替换 3 个列条目,假设有 3 条 id 为 1 2 3 的记录)。

    编辑:要获得实际列值,您必须确保 city 列采用列表(即,将关键字 meta 应用于您的表应该在 t 列中返回大写 S city.

    假设您的city 列在运行meta user 时当前有一个小的s,您可以通过运行来更新它:

    update city:enlist each city from `user

    然后,以下将进行您想要的更改:

    update city:city:\:enlist`london from user where id in 1 2 3

    【讨论】:

    • 这可行,但 `london 值不在列表中。我应该在数据库中看到 enlist london。
    • 啊,我明白了——编辑了上面的城市列值作为列表^
    【解决方案2】:

    如果您希望它保留为列表,您可以执行类似的操作

    {update city:(x)#enlist `london from `user where id in 1 2 3}count select from t where id in 1 2 3
    

    或者整理一下,你可以使用

    update city:count[i]#enlist `London from t where id in 1 2 3
    

    由于在 where 子句中进行过滤,计数中的 i 将与列表长度相同。

    【讨论】:

      【解决方案3】:

      您也可以使用?[boolean_list;if_true;if_false] 运算符,如下所示:

      / this is just to create a test table
      t:([] id:(1;2;3;4); city:4#`)
      
      / check if each element of t`id is in (1;2;3)
      / 1) `London if true, ` if false
      / 2) assign t[`city]
      t[`city]:?[in[t`id;(1;2;3)];`London;`]
      

      【讨论】:

        【解决方案4】:

        我可以通过以下方式做到这一点。

        update city: 3#enlist enlist `Lodnon from `user where id in (1,2,3);
        

        【讨论】:

        • 您的答案可以通过添加有关您的代码的作用以及它如何帮助 OP 的更多信息来改进。
        猜你喜欢
        • 2020-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多