2015-08-27 64 views
0

我使用下面庫:[org.postgresql/postgresql "9.4-1201-jdbc41"]連接到我的數據庫,我使用下面的查詢查詢:Clojure的Postgres的SQL查詢忽略SELECT排序

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 [])把每個結果放到一個數組,應該維持排序?

+0

我還沒有嘗試過這一點,但我認爲問題是結果已經是無序的地圖。因此,對已經無序的東西應用(到[])將不會給你正確的順序。 –

+0

@ViktorK。是的,我認爲你的正確,任何想法如何命令查詢? –

回答

2

我找到的解決方案是使用[clojure.java.jdbc]庫並添加:as-arrays? true它按順序返回結果。