0

by right SELECT topicFollowed FROM User WHERE id = 10000) return "[0,2]" <<-string

but when i execute SELECT * from News WHERE topicId IN (SELECT topicFollowed FROM User WHERE id = 10000) only 0 is pass in and 2 is not. i cant figure out why. it only return topicId that is 0

1 Answer 1

1

Your IN list consists of exactly one item, a string of the form '[0,2]'. If that is what topicId looks like, then your query will work.

In other words, you have a problem with your data structure. You shouldn't be storing lists of things in strings. The proper data structure would have two rows, one for each of the ids in the list.

Sometimes, you are stuck with someone else's bad data design. You can do what you want, by doing something like this:

SELECT n.*
from News n
WHERE EXISTS (SELECT 1
              FROM user u
              WHERE find_in_set(topicId, replace(replace(topicFollowed, '[', ''), ']', '') AND
                    id = 10000
             );
Sign up to request clarification or add additional context in comments.

2 Comments

I am storing array in varchar, is this the correct way or there is a better way to do this?
@phongyewtong . . . Yes, use a junction table, with one row per id and one row per topic id.

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.