Union in HoneySQL

417 views Asked by At

Would like to make a UNION (MySQL, but PostgreSQL has them too) in a HoneySQL query. Searching the source code suggests it might be possible, but does not suggest an obvious approach to try.

1

There are 1 answers

0
Logan Buckley On BEST ANSWER

There appears to be no helper function, but you can use sql/build:

(sql/format (sql/build :union [(-> (select :*)
                               (from :table1))
                           (-> (select :*)
                               (from :table2))]))

=> ["(SELECT * FROM table1) UNION (SELECT * FROM table2)"]

This helper could work, although it's not quite idiomatic with the other helpers:

(defhelper union [m1 m2]
  {:union [m1 m2]})

(sql/format (union (-> (select :*) (from :table1))
                   (-> (select :*) (from :table2)))