如何从 table 中删除多列?

How do I remove multiple columns from a table?

所以有 delete col from table 删除单个列。我想我可以使用 over 来删除多列。但是:

您可以像删除多列一样删除多列select多列。

delete col1,col2 from table

这种情况下用over肯定效率低

然而,在某些示例中,您可能希望将列名作为符号传递到执行 select 或删除的函数中。

这样做需要使用删除的函数形式:https://code.kx.com/q/ref/funsql/

功能性删除示例

q)table:([]col1:1 2 3;col2:1 2 3;col3:10 20 30) 
q)//functional delete
q){![table;();0b;x]} `col1`col2
col3
----
10  
20  
30  
q)//inplace functional delete
q){![`table;();0b;x]} `col1`col2
`table
q)table
col3
----
10  
20  
30 

我不能在etc211的解决方案下面发表评论,所以我刚刚开始另一个答案post。

Hmm, functional delete doesn't seem to work when the list of columns is empty. I'd expect that not to touch the table at all, and yet it deletes all the rows in it instead. 

对于上面的内容,您为什么不创建一个函数来选择您愿意删除的列?

假设您的 table t 包含列名:col1,col2,col3,col4 并且您想删除:col5,col6

来自q码:

tgt_cols:`col5`col6;
filtered_cols: (cols t) inter tgt_cols;
if[0 < count filtered_cols;
    {![`t;();0b;x]} filtered_cols];

以上将首先检查您要删除的列是否存在;如果要删除的目标列存在,它将删除这些列。

对于内存中 table 你也可以使用 drop: http://code.kx.com/q/ref/lists/#_-drop

q)((),`col1)_table
col2 col3
---------
1    10
2    20
3    30
q)((),`col1`col3)_table
col2
----
1
2
3
q)((),`)_table
col1 col2 col3
--------------
1    1    10
2    2    20
3    3    30