我有一个表,在这个表中,我想根据一个条件更新一行中的几列。
q)t:([] id:10 20; l1:("Blue hor";"Antop"); l2:("Malad"; "KC"); pcd:("NCD";"FRB") )
当我使用更新语句时,它抛出'长度错误'。
q)update l1:"Chin", l2:"Gor" from t where id=10
'length
q)update l1:"Chin", l2:"Gor" from `t where id=10
'length
我在下面读到 Q for Mortals
但是有什么方法可以根据条件更新一行中的几列?
Where短语和Update短语中的操作是对整个列列表的向量操作。这就是更新的禅。
由于你在这里处理的是Char-list(而不是符号),你需要使用enlist。
q)update l1:enlist "Chin", l2:enlist "Gor" from t where id=10
id l1 l2 pcd
----------------------
10 "Chin" "Gor" "NCD"
20 "Antop" "KC" "FRB"
否则你就会试图更新一个长度为1的向量(t where id=10
),向量长度为4("Chin"
),或3 ("Gor"
).
请尝试下面的语句。
update l1:count[i]#enlist"Chin", l2:count[i]#enlist"Gor" from t where id=10
无论where子句匹配了多少行,它都能正常工作。
在更新时,分配的列表长度应该等于更新的行数。Q
处理 string
作为字符列表。这就是为什么当你将 "Chin"
到L1。Q
试图分配长度为4的列表,而预期长度为1的列表。这将导致 'length
错误。
count[i]#enlist"Chin"
创建N个重复值的列表。("Chin";"Chin";...)
. 其中N是更新的行数。这就解决了
要更新这样的表,你需要添加enlist关键字。
q)update l1:enlist "Chin", l2:enlist "Gor" from t where id=10
id l1 l2 pcd
----------------------
10 "Chin" "Gor" "NCD"
20 "Antop" "KC" "FRB"
这是因为你需要添加的是字符串的列表,而不是单纯的字符串。