MySQL : Select 最后三项 INNER JOIN

MySQL : Select three last item INNER JOIN

只是用 MySQL 来一个棘手的阻塞。 我有 3 张桌子 :

电视

| TV_ID  |  TV_name  |
----------------------
|    1   |    HBO    |
|    2   |    BBC    |
|    3   |  Fox news |
----------------------
----------------------
----------------------

发射

| 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   |
-------------------------------
-------------------------------
-------------------------------

回放

| 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    |
-------------------------------------------
-------------------------------------------
-------------------------------------------

而且我只想创建一个查询来获取每台电视的第三次最后发射,按日期和电视排序(如果可能)。 所以对于这个例子,我有 3 台电视。 3*3 = 9 次发射,例如:

| 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     |

我尝试了很多解决方案 (INNER JOIN -- SELECT .. FROM ( SELECT ...) -- 使用 var @:= -- Sub-sub-sub-sub查询)但不起作用.. 只有用UNION才行,可是我有20多台电视,写20个UNION实在是太急了..

如有建议,

提前致谢,

这并不简单,但简而言之,按电视和日期对您的重播进行排序,然后对它们进行排名,然后 select 符合您的排名标准的那些。

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

demo here