1

I have a table letter_count looking like this:

count   letters
2   ["a","b"]
4   ["b","c"]
2   ["a"]
0   ["b"]
6   ["c"]
7   ["d"]

I'd like to be able to query only the rows containing a specific letters.

I have tried:

SELECT * FROM letter_counter WHERE letters IN ('["a"]')

But it fails.

What's the right syntax for this here?

1
  • 2
    Serialised data in a database column is a very bad idea, at least in part for the very issue you're encountering now. If you have control over the database schema you should refactor it to use a dependant table for the letters. Commented Aug 15, 2016 at 8:42

2 Answers 2

2

try this select:

SELECT * FROM letter_counter 
WHERE(case when position('a' in array_to_string(letters,',')) > 0 then true else false end)
Sign up to request clarification or add additional context in comments.

Comments

1

Use this:

SELECT * FROM letter_counter WHERE letters @> ARRAY['a']::varchar[]

Or you could spend 5 seconds searching Stack Overflow and just use this answer.

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.