我一直在尝试在 Korma 中执行以下操作但无济于事:
sql:
PREPARE q (int) AS SELECT * FROM post a
WHERE EXISTS
(SELECT parent_id
FROM post_map
WHERE parent_id=a.id AND parent_id=$1);
EXECUTE q(1);
我最好的 Korma 尝试:
(defn children [parent-id] ;; clojure
(if (number? parent-id)
(exec-raw (str
"PREPARE q (int) AS SELECT * FROM post a WHERE EXISTS
(SELECT parent_id FROM post_map WHERE parent_id=a.id AND parent_id=$1);
EXECUTE q(" parent-id ")")
:results)))
这是我不断收到的错误:( 我不太了解::
下面的运算符:)
Failure to execute query with SQL:
PREPARE q (int) AS SELECT * FROM post a WHERE EXISTS
(SELECT parent_id FROM post_map WHERE parent_id=a.id AND parent_id=$1);
EXECUTE q(1) :: nil
PSQLException:
Message: No results were returned by the query.
SQLState: 02000
Error Code: 0
PSQLException No results were returned by the query. org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery (AbstractJdbc2Statement.java:274)
我不认为这对查询来说是一件非常奇怪的事情,所以我想知道 Korma 是否不适合我的项目。我只是做错了吗?
更新:这就是我最终做的事情(在我放弃 Korma [对不起 Korma] 之后)。
(defn children [parent-id]
(if (unsigned? parent-id)
(sql/with-connection db
(sql/with-query-results results
[(str "select " field-list ", b.parent_id from post a, post_map b where a.id=b.child_id and a.id in "
"(select child_id from post c, post_map d where c.id=d.parent_id and c.id=?)") parent-id]
(into [] results)))))