4

I have a user_lists table that contains a user_list column of type integer[].

I'm trying to do this query, which seems basic enough:

select id, alias from users where id = ANY(select user_list from user_lists where id = 2 limit 1);

It gives this error:

ERROR: operator does not exist: integer = integer[]
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.

I'm using postgres 8.3.11. Upgrading is not an option.

What am I missing?

2 Answers 2

6

Try this instead:

select id, alias from users 
where (select user_list from user_lists where id = 2 limit 1) 
@> ARRAY[id];
Sign up to request clarification or add additional context in comments.

2 Comments

Whoah. Magic. Care to explain this?
ARRAY_1 @> ARRAY_2 means "ARRAY_1 contains ARRAY_2"
1

You can also try something like this (ought to work on 8.3, don't have one to hand):

SELECT u.id, u.alias 
FROM users u JOIN user_lists ul ON u.id = ANY(ul.user_list) 
WHERE ul.id = 2;

Oh, you're missing some bugfixes (8.3.18 is current) and I'd expect 8.3 to be end-of-life soon, so upgrading really needs to be an option in the next year or so.

Comments

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.