开发者

Postgres searching in a column splitted by regexp_split_to_table

I have the following table:

cs_id ; cs_values ; cs_desc
---------------------------
   开发者_如何学编程  1;    'a,b,c';   'one'
     2;      'd,a';   'two'
     3;      'a,c'; 'three'

The field "cs_valies" contains different comma separeted values. I would like to get all "cs_id" on lines that contain a certain value in "cs_values".

I used this expression:

SELECT
   cs_id,
   regexp_split_to_table(cs_values, '* , *') as splitted_value
WHERE
   splitted_value = 'a'

I have to questions:

  1. Postgres does not like alias names in the WHERE-clause. Or has anyone an idea how to achieve this?
  2. Does anyone have a better idea to solve the given problem?

Thanks everybody, I hope I am not missing something extremly evident.


Postgres does not like alias names in the WHERE-clause. Or has anyone an idea how to achieve this?

SELECT * 
FROM (
   SELECT 
      cs_id,
      regexp_split_to_table(cs_values, '* , *') as splitted_value
) t
WHERE
   splitted_value = 'a'

Does anyone have a better idea to solve the given problem?

Normalize your table and put the "comma separated list" into its own table with. This is a classical 1:n relation


I had a similar situation on my hands. Here's what I found works with pgsql:

select a.id, a.name from student as a, (select id, cast(regexp_split_to_table(topics, ',') as text) as topic from student) as b where a.id = b.id and b.topic = 'science';

Hope it helps you too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜