2017-10-20 138 views
1

在我的表中,我有一個名爲facebook的列,其中的數組類型爲text []。例如:Postgres按數組值排序

{{total_count,26861},{comment_count,94},{comment_plugin_count,0},{share_count,26631},{reaction_count,136}} 

我用我的數據庫,數據表,當我試圖通過Facebook來我的表進行排序我有這樣的:

enter image description here

這是不對的。所以我試圖從這個數組中只獲取total_count作爲數值。現在我有這樣的:

regexp_matches(array_to_string(facebook, ' '), '(\d+).*') 

但這返回數組,例如:

enter image description here

所以我增加::數字

regexp_matches(array_to_string(facebook, ' '), '(\d+).*')::numeric 

,但我出現錯誤:

cannot cast type text[] to numeric

任何想法如何解決它?

+1

@ lad2025它是如此簡單...非常感謝! –

+0

當然,我發佈它作爲答案:) – lad2025

回答

1

cannot cast type text[] to numeric

你需要轉換爲numeric[]

regexp_matches(array_to_string(facebook, ' '), '(\d+).*')::numeric[] 
+1

就是這樣!現在它的工作。 –

+0

@DanielKoczuła請考慮接受我的回答[接受答案如何工作?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235):) – lad2025