Clojure postgres sql查询忽略了SELECT排序

时间:2015-08-27 14:49:05

标签: postgresql clojure

我使用以下库:[org.postgresql/postgresql "9.4-1201-jdbc41"]连接到我的数据库,我使用以下查询查询:

select
    happenedat,
    "_customer.email",
    ismobile,
    "_customer.title",
    "_customer.firstname",
    "_customer.lastname",
    "_eventdata.park.name",
    "_customer.address line 1",
    "_customer.address line 2",
    "_customer.address town",
    "_customer.address county",
    "_customer.postcode",
    "_customer.telephone number"
from my_table
where eventaction = 'brochure request'
and happenedat > (getdate() - 5)
and happenedat < getdate()
and ("_customer.email" is not null or "_customer.firstname" is not null or "_customer.lastname" is not null or "_customer.postcode" is not null)
order by 7,1

如果我使用某个应用程序运行此代码,例如Postico,则查询将按预期返回,但是如果我使用Clojure运行此操作,则执行以下操作:

defn write-query-to-csv [query db output-filename]
  (log/info (str "Executing " query " on " db))
  (let [results (query db)
        header (->> results
                    first
                    keys
                    (map name)
                    (into []))
        data (->> results
                  (map #(vec (vals %))))]
    (with-open [out-file (io/writer output-filename)]
      (csv/write-csv out-file
                     (reduce conj (conj [] header) data)))
    (io/file output-filename)))

将查询写入CSV,但SELECT字段的顺序完全错误,不保留这些字段的顺序。

我在这里读到:Similar error因为结果是作为无序地图返回的,你需要将它们作为数组返回,但不是(into [])将每个结果放入数组中哪个应该保持排序?

1 个答案:

答案 0 :(得分:2)

我找到的解决方案是使用[clojure.java.jdbc]库并添加:as-arrays? true,然后按顺序返回结果。