Question

I have text field in my pgsql DB which contains users identificators. One field can contain one or more ids separated by #. On saving numbers are imploded using PHP, so I can have for example:

Row 1: 3#36#66#33

Row 2: 5#56#33#55.

How should I prepare SQL query to select field with one or more ids without any ambiguity?

Example:

SELECT product FROM cms_product WHERE product_mod LIKE '3'

Both row 1 and 2 can be selected which I don't want. How I can fix it?

EDIT:

I will have to select all rows that contains user ID in this filed, for example:

UserId: 3
Field 1:
55#64#333#2
Field 2:
12#3#55#6423
Field 3;
654#33#11#98

Only field 2 should be selected.

Was it helpful?

Solution

Although you should really work to normalize your DB this is the answer to the posted question

select product
from cms_product
where '3' = any(regexp_split_to_array(product_mod, '#'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top