MySQL : Select three last item INNER JOIN -


this question has answer here:

just have tricky blocking mysql. i've 3 tables :

tv

| tv_id  |  tv_name  | ---------------------- |    1   |    hbo    | |    2   |    bbc    | |    3   |  fox news | ---------------------- ---------------------- ---------------------- 

emission

| e_id | e_tv_id |   e_name   | ------------------------------- |   1  |    1    |   weather  | |   2  |    1    |   news     | |   3  |    1    |   fake1    | |   4  |    1    |   fake2    | |   5  |    1    |   fake3    | |   6  |    1    |   fake4    | |   7  |    2    |   fake5    | |   8  |    2    |   fake6    | |   9  |    2    |   fake7    | |   10 |    2    |   fake8    | |   11 |    2    |   fake9    | |   12 |    2    |   fake10   | |   13 |    2    |   fake11   | |   14 |    3    |   fake12   | |   15 |    3    |   fake13   | |   16 |    3    |   fake14   | |   17 |    3    |   fake15   | |   18 |    3    |   fake16   | |   19 |    3    |   fake17   | |   20 |    3    |   fake18   | ------------------------------- ------------------------------- ------------------------------- 

replay

| r_id | r_e_id |  r_date  | r_url_replay | ------------------------------------------- |  1   |   1    | 20150431 |     url1     | |  2   |   20   | 20150431 |     url2     | |  3   |   19   | 20150431 |     url3     | |  4   |   2    | 20150431 |     url4     | |  5   |   7    | 20150431 |     url5     | |  6   |   16   | 20150430 |     url6     | |  7   |   10   | 20150430 |     url7     | |  8   |   1    | 20150430 |     url8     | |  9   |   4    | 20150430 |     url9     | |  10  |   9    | 20150430 |     url10    | |  11  |   19   | 20150429 |     url11    | |  12  |   2    | 20150429 |     url12    | |  13  |   1    | 20150429 |     url13    | |  14  |   12   | 20150429 |     url14    | ------------------------------------------- ------------------------------------------- ------------------------------------------- 

and want create 1 query 3rd last emission of each tv, order date , tv (if possible). exemple, i've 3 tv. 3*3 = 9 emissions, :

| tv_id  |  e_name   | r_url_replay | ------------------------------------- |    1   |  weather  |     url1     | |    1   |   fake2   |     url4     | |    1   |   fake6   |     url8     | |    2   |   fake3   |     url5     | |    2   |   fake8   |     url7     | |    2   |   fake7   |     url10    | |    3   |   fake18  |     url2     | |    3   |   fake17  |     url3     | |    3   |   fake14  |     url6     | 

i've try many solution (inner join -- select .. ( select ...) -- use var @:= -- sub-sub-sub-sub query ) not works.. works if use union, i've more 20 tv, , write 20 union urgly..

if have suggestion,

thanks in advance,

it's not straightforward, in nutshell, sort replays tv , date, rank them, select match rank criteria.

select *    (     select if(@prev = e_tv_id, @rank := @rank +1, @rank := 1 , @prev := e_tv_id) rank, q.*       (          select e.e_tv_id, r_date, r_url_replay           emission e           join (select @prev := 0, @rank := 1) q             inner join replay r             on r.r_e_id = e.e_id         order e.e_tv_id asc, r.r_date desc      ) q   ) qq rank <=3 ; 

demo here


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -