mysql - SQL query - Selecting records based on count condition -


the title may not seem clear - not sure how explain problem in 1 line.

i have 3 tables

 topic video  topic_video 

a topic can have 1 or 2 videos. videos either sample videos or not.

sample data in tables relevant column names

topic

 topic_id |         name          | course_id     1    | excel - add/subtract  |     1     2    | excel - sort          |     1     3    | excel - filter        |     1     4    | excel - formulas      |     1   video  video_id | video_url     10   | www.youtube.com?v=123     12   | www.youtube.com?v=345     13   | www.youtube.com?v=567     14   | www.youtube.com?v=879     15   | www.youtube.com?v=443   topic_video  topic_video_id | topic_id | video_id | is_sample           1        |    1    |    10    |    y       2        |    2    |    12    |    n       3        |    3    |    13    |    n       4        |    3    |    14    |    y       5        |    4    |    15    |    n  

so trying given course select topics , corresponding videos. if topic has more 1 video - want select video is_sample "n".

like in above example - topic_id 3 has 2 videos video_id 13 , 14 - video_id 13 should selected.

this query have far

 select topic.*,count(topic.topic_id),video.video_id,topic_video.is_sample topic left join topic_video on topic_video.topic_id = topic.topic_id left join video on video.video_id = topic_video.video_id course_id=1 group topic.topic_id 

so if count 2 - want select record is_sample='n' possible , how can accomplish this. lot time

one way solve

  • join inline view calculates count topic.
  • use count create case statement joins is_sample = n


select *    topic         left join (select topic_id,                           count(topic_id) t_count                      topic_video                    group  topic_id) t                on topic.topic_id = t.topic_id         left join topic_video                on topic_video.topic_id = topic.topic_id                   , topic_video.is_sample = case                                                 when t.t_count = 2 'n'                                                 else topic_video.is_sample                                               end         left join video                on video.video_id = topic_video.video_id  

demo

output

| topic_id |                 name | course_id | t_count | topic_video_id | video_id | is_sample |             video_url | ------------------------------------------------------------------------------------------------------------------------- |        1 | excel - add/subtract |         1 |       1 |              1 |       10 |         y | www.youtube.com?v=123 | |        2 |         excel - sort |         1 |       1 |              2 |       12 |         n | www.youtube.com?v=345 | |        3 |       excel - filter |         1 |       2 |              3 |       13 |         n | www.youtube.com?v=567 | |        4 |     excel - formulas |         1 |       1 |              5 |       15 |         n | www.youtube.com?v=443 | 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -