gpt4 book ai didi

postgresql - CLSQL 中带有 select 函数的子查询

转载 作者:行者123 更新时间:2023-11-29 11:29:44 24 4
gpt4 key购买 nike

我正在尝试使用 clsql:select 函数创建子查询:

CL-USER> (select [books.bookid] 
:from [books]
:where
(sql-in [books.bookid]
(select [bookid]
:from [bookauthors]
:where
(sql-= [bookauthors.authorid] 120))))
;; 2015-03-07T06:37:08 /books/ => SELECT BOOKID FROM BOOKAUTHORS WHERE (BOOKAUTHORS.AUTHORID = 120)
;; 2015-03-07T06:37:08 /books/ => SELECT BOOKS.BOOKID FROM BOOKS WHERE (BOOKS.BOOKID IN ((157)))
((157))
("bookid")

它有效,但 clsql 运行两个查询,而不是生成一个带有子选择子句的查询。这不会像让 postgresql 后端处理整个事情那样高效。

CL-USER> (clsql-sys:db-type-has-subqueries? :postgresql)
T

显然 postgresql 连接器支持子查询。有没有办法让 select 函数生成它们?

最佳答案

在上面的调用中,您实际上是在运行内部选择,然后将结果拼接到外部调用中。

您应该使用 sql 表达式而不是函数。如果你(clsql-sys:file-enable-sql-reader-syntax)这可以用方括号完成,如下所示。


(select [books.bookid]
:from [books]
:where
[in [books.bookid]
[select [bookid]
:from [bookauthors]
:where
[= [bookauthors.authorid] 120]]))


此外,您可能希望使用 :postgresql-socket3后端,因为它是三个 postgresql clsql 后端中最强大/最新的(它使用 cl-postgresql 提供的库 postmodern 通过其套接字 api 的版本 3 访问 postgresql。:posgresql-socket 使用 postgres 套接字的版本 2 api,:postgres 通过 C 客户端使用 FFI。

关于postgresql - CLSQL 中带有 select 函数的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28916811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com