How to count the number of terms in a table with postgresql?

对着背影说爱祢 提交于 2019-12-12 04:52:55

问题


alldp charcter varraying

"K409,K358,K353,L059,R100,L050,K610,K352,K429,C181,D129,K565,T8558,K560,K402,A045,K661,I841,I848,Z433"

I can count them. here is 20.

But if I want to write a statement in Postgres to get 20, how should I do?


回答1:


You can split the string into an array and then count the elements in the resulting array:

select array_length(string_to_array(the_column, ','), 1)
from the_table;

But you should really consider fixing your data model. Storing comma separated values in a column is almost always a bad idea.



来源:https://stackoverflow.com/questions/27128537/how-to-count-the-number-of-terms-in-a-table-with-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!