按MAX号加入两个表

时间:2018-02-01 20:45:55

标签: mysql sql left-join inner-join right-join

我有两张这样的桌子;

|episodes
|-----------|------|------|-------|-------|
|id|movie_id|title |season|episode|scraped|
|-----------|-------------|---------------|
|1 |22      |ep1   |5     |1      |0      |
|2 |22      |ep2   |6     |1      |0      |
|3 |33      |ep1   |7     |1      |0      |
|4 |33      |ep2   |7     |2      |0      |
|-----------------------------------------|


|pages                                                    
|----------------|------------|
|pid |  imdb_id  |  imdb_title|
|----------------|-------------
|11  |  X-Files  |  imdb1     |
|22  |  Seinfeld |  imdb2     |
|33  |  Lost     |  imdb3     |
|-----------------------------|          

我想根据seasons的唯一MAX数将两个表合并为一个。例; Seinfeld的最后一个赛季是6,而Lost唯一的赛季是7。

表格最终版本应该是这样的。

|-----------|-----|------|-------|-------|---|--------|----------|
|id|movie_id|title|season|episode|scraped|pid| imdb_id|imdb_title|
|-----------|-----|------|---------------|---|--------|----------|
|3 |33      |ep1  |7     |1      |0      |11 |Lost    |imdb3     |
|4 |33      |ep2  |7     |2      |0      |11 |Lost    |imdb3     |
|2 |22      |ep2  |6     |1      |0      |22 |Seinfeld|imdb2     |
|----------------------------------------------------------------|

我尝试了此查询,但无法包含pages表。

SELECT a.*
FROM episodes a
INNER JOIN (
    SELECT movie_id, MAX(season) season
    FROM episodes
    GROUP BY movie_id
) b ON a.movie_id = b.movie_id AND a.season = b.season
WHERE a.scraped = '0'

后来尝试了另一个查询,这次添加了页面表,但这个查询比以前慢得多。

SELECT a.*, c.pid AS page_id, c.imdb_id, c.imdb_title 
 FROM episodes a    
 INNER JOIN pages AS c ON c.id = a.movie_id                                        
 LEFT OUTER JOIN episodes b ON a.movie_id = b.movie_id AND a.season < b.season                                        
 WHERE b.movie_id IS NULL and a.scraped = '0' 

我知道,有点困惑,但我希望能正确解释。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你应该通过movie_id

加入max(季节)组
select e.id, e.movie_id, e.title, e.season, e,epison, p.pid, p.imdb_id, b.imdb_title
from episod 2 
inner join pages p on p.pid = e.movie_id
inner join (
  select movie_di, max(season) as max_season
  from episodes 
  group by movie_id
) t on t.movie_id = e.movie_id and t.max_season = e.season