How to count the number of terms in a table with p

2019-09-04 10:02发布

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条回答
够拽才男人
2楼-- · 2019-09-04 10:12

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.

查看更多
登录 后发表回答