1

我想为此查询中的子选择分配一个别名:

(->
  (korma/select* table_a)
  (korma/join
   (korma/subselect table_b
                    (korma/fields :id (korma/raw "COUNT(*) AS count"))
                    (korma/group :id))
   (= :id :id)))

这是我要创建的 sql:

SELECT * FROM table_a LEFT JOIN (SELECT id, COUNT(*) AS count FROM table_b GROUP BY id) AS b b.id = id;

但目前 sql korma 刚刚生成:

SELECT * FROM table_a LEFT JOIN (SELECT id, COUNT(*) AS count FROM table_b GROUP BY id) id = id;
4

1 回答 1

0

我需要将子选择定义为一个单独的实体,见下文:

(korma/defentity sub-select
             (korma/table (korma/subselect table_b
                                           (korma/fields :id(korma/raw "COUNT(*) AS count"))
                                           (korma/group :id)) :b))

(->
  (korma/select* table_a)
  (korma/join sub-select (= :b.id :id)))

这会生成以下查询:

SELECT * FROM table_a LEFT JOIN (SELECT id, COUNT(*) AS count FROM table_b GROUP BY id) a.id = id;
于 2018-06-01T10:58:36.890 回答