MySQL:选择三个最后一项INNER JOIN

时间:2015-04-29 11:52:51

标签: mysql inner-join union var multiple-tables

使用MySQL进行棘手的阻止。 我有3张桌子:

TV

| 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真是太难看了..

如果您有建议,

提前致谢,

1 个答案:

答案 0 :(得分:0)

这不是直截了当的,但简而言之,按电视和日期排序重播,然后对它们进行排名,然后选择符合排名标准的重播。

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