2

here i have 2 table in database like this,

1)videos

id        name   
200119    one
200120    two
200121    three

2)sessions_to_channels

channel_id    playlist_id   videos
50            359           200119,200120,200121

I want to select all the videos.name from videos table where videos.id IN sessions_to_channels.videos.

For that i am using this query,

SELECT v.name FROM videos as v WHERE v.id in ( select videos from sessions_to_channels where playlist_id=359 and channel_id=50 )

But it return me only 1 record

id      name
200119  one

I am doing anything wrong here?

6
  • 1
    Instead of executing the query, try and echo it and see if the syntax is correct. Commented Sep 8, 2015 at 9:22
  • @Epodax only issue is, it returns only 1 record. when i put sub-query result into IN clause, then it return me correct result Commented Sep 8, 2015 at 9:24
  • Then I'm not sure what the issue is? Commented Sep 8, 2015 at 9:25
  • 4
    Your query isn't going to work as is, because you are trying to find a value in a comma-separated string. In your previous question today, you were advised to not use this database design. Now the roosters are coming home to roost. Commented Sep 8, 2015 at 9:27
  • @TimBiegeleisen you are right, but this database designed by other. i have only option to use it any how Commented Sep 8, 2015 at 9:28

2 Answers 2

1

If you use string in sessions_to_channels.videos, you must use FIND_IN_SET. Like

SELECT v.name
FROM videos AS v
WHERE FIND_IN_SET(v.id, (
    SELECT videos FROM sessions_to_channels WHERE playlist_id=359 and channel_id=50 )
)
Sign up to request clarification or add additional context in comments.

Comments

0

Saving comma-separated data is never a good choice and the best option is to normalize the table. This will definitely make your life easy, however for the current situation you can use find_in_set function something as

SELECT v.name
FROM videos v JOIN sessions_to_channels s
ON FIND_IN_SET(v.id,s.videos) > 0 
WHERE s.playlist_id = 359 AND s.channel_id = 50;

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.