使用 like 搜索多个字符串

Searching multiple strings using like

如何使用 like 搜索多个字符串?

q)\l sp.q

q)select from p where city like "lon*"
p | name  color weight city
--| -------------------------
p1| nut   red   12     london
p4| screw red   14     london
p6| cog   red   19     london

我想搜索以 "lon" 或 "par" 开头的 city/: 给出 type 错误。

q)select from p where city like/: ("lon*";"par*")
'type

搜索多个字符串时需要使用any

q)select from p where any city like/: ("lon*";"par*")

p | name  color weight city
--| -------------------------
p1| nut   red   12     london
p2| bolt  green 17     paris
p4| screw red   14     london
p5| cam   blue  12     paris
p6| cog   red   19     london

当您使用 /: 搜索时(每个正确),它 returns 2 个向量,一个针对 "lon*" 搜索,另一个针对 "par*".

(0!p)[`city] like/: ("lon*";"par*")
(100101b;010010b)

使用 any 进行 ORing 和 returns 单个向量。

any (0!p)[`city] like/: ("lon*";"par*")
110111b

现在得到最终结果:

(0!p) where any (0!p)[`city] like/: ("lon*";"par*")
p  name  color weight city
----------------------------
p1 nut   red   12     london
p2 bolt  green 17     paris
p4 screw red   14     london
p5 cam   blue  12     paris
p6 cog   red   19     london

您需要在 where 子句中添加 any。现在您的 where 子句正在解析为 2 个列表

q)city:`london`london`newyork`paris
q)city like/: ("lon*";"par*")
1100b
0001b
q)any city like/: ("lon*";"par*")
1101b

所以...

select from p where any city like/: ("lon*";"par*")

正如其他人所提到的,使用 anyeach-right 是这里最好的方法,也是最易读的。

另一种方法(在某些情况下可能很有用,并且对于大型向量可能更有效)涉及使用内置的正则表达式功能,如下所示:

q)city:`london`london`newyork`paris
q)city like "[lp][oa][nr]*"
1101b

这里需要注意的是,它还会选择 "lan*""lar*""por*""pan*",但如果已知这些组合是不可能的,那么这是可行的。