代码之家  ›  专栏  ›  技术社区  ›  Freid001

sqlkoram-子选择上的join,但如何指定别名?

  •  1
  • Freid001  · 技术社区  · 6 年前

    我要为此查询中的子选定对象指定别名:

    (->
      (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;
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Freid001    6 年前

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

    (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;