0

I have the following two tables in Postgres database

employee

emp_id usersname first_name email country
251030 username1 first_name_1 [email protected] USA
251035 username2 first_name_2 [email protected] Canada
251036 username3 first_name_3 [email protected] Australia

employee_notifications

id notification_title users(jsonb)
1 title1 [251030,251035]
2 title2 [251030,251035]
3 titl3 [251030,251035]

Now I need to select the employee details from the employee table where the values are in users array in employee_notifications table. The users column is of type jsonb

I am using the following query here :

SELECT emp_id,username FROM employee 
WHERE emp_id IN (
    SELECT users FROM employee_notifications WHERE id =3
)

When I use this, I am getting an error ERROR: operator does not exist: character varying = jsonb. Also please note that my emp_id column is of type varchar

How can I convert this jsonb array to a list or something and use it in the IN condition ?

5
  • 1
    Why using JSON for data to be queried? Far from optimal performance design.. Commented Feb 4, 2022 at 7:22
  • Does this answer your question json array into rows Commented Feb 4, 2022 at 7:25
  • 2
    Use a "junction table" instead of a JSON array, then it will be a simple join. Commented Feb 4, 2022 at 7:41
  • 1
    Storing numbers in varchar columns is also a bad decision. Commented Feb 4, 2022 at 7:53
  • I agree with all the comments here regarding the design. But the table is already existing there and I am not able to change the design as of now as it requires a lot of effort to refactor the complete application. This will be done later, but as of now, I need some workaround to get eh values. Commented Feb 4, 2022 at 8:56

1 Answer 1

0

users is an array of IDs, so you can't compare it with a single integer value. You will need to use an EXISTS condition:

SELECT e.emp_id, e.usersname
FROM employee e
WHERE EXISTS (
    SELECT * 
    FROM employee_notifications en
    WHERE en.users @> to_jsonb(e.emp_id)
)

If employee.emp_id is indeed a varchar (why?), and your JSONB contains "integer" values like this :[251030,251035] (why if emp_id is a string?) you need to cast the emp_id to an integer before converting it to a JSONB value:

SELECT e.emp_id, e.usersname
FROM employee e
WHERE EXISTS (
    SELECT * 
    FROM employee_notifications en
    WHERE en.users @> to_jsonb(e.emp_id::int)
)
Sign up to request clarification or add additional context in comments.

2 Comments

This is just returning the column names in the select statement and there is no values even though I have matching values there in the array and the employee table.
@HappyCoder: see my edit you are using inconsistent data types all over so you need to cast things back and forth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.