我正在尝试从数据库中获取所有用户,除了那些与一组UID相关的用户。我已经编写了这个查询,但是当UID列表包含多个元素时,由于某些原因,“not”子句不起作用,它会返回所有注册用户。
(d/q '[:find (pull ?e [*])
:in $ [?uids ...]
:where [?e :user/id ?uid]
(not [?e :user/id ?uids])]
db ["user-uid-1" "user-uid-2" "user-uid-3"])当UID的列表包含单个元素时,查询将正常工作(它将返回所有用户,但具有指定UID的用户除外)。
有什么想法可能是错误的吗?
发布于 2021-09-23 21:16:56
使用[?uids ...]的行为类似于SELECT * FROM user WHERE id != uid1 UNION SELECT * FROM user WHERE id != uid2,而不是预期的SELECT * FROM user WHERE id NOT IN (uids)
例如,在下面的示例查询中,尝试获取除苹果或梨以外的所有水果
(d/q
'[:find ?id ?fruit ?fruits
:in $ [?fruits ...]
:where
[?id :fruit ?fruit]
(not [?id :fruit ?fruits])]
[[1 :fruit :apple]
[2 :fruit :orange]
[3 :fruit :pear]]
[:apple :pear])
; => #{[3 :pear :apple] [2 :orange :pear] [1 :apple :pear] [2 :orange :apple]}我们看到,查询针对列表中的每个水果运行。:pear给了[2 :orange :pear] [1 :apple :pear],:apple给了[3 :pear :apple] [2 :orange :apple]。
为了找到不在集合中的所有项,您需要将集合设置为一个集合,并以标量绑定的形式发送它,如下所示
(d/q
'[:find ?id ?fruit ?fruits
:in $ ?fruits
:where
[?id :fruit ?fruit]
(not [(?fruits ?fruit)])]
[[1 :fruit :apple]
[2 :fruit :orange]
[3 :fruit :pear]]
#{:apple :pear})
; => #{[2 :orange #{:apple :pear}]}在本例中,您需要重写查询,如下所示
(d/q '[:find (pull ?e [*])
:in $ ?uids
:where [?e :user/id ?uid]
(not [(?uids ?uid)]
db #{"user-uid-1" "user-uid-2" "user-uid-3"})https://stackoverflow.com/questions/68529942
复制相似问题