MySql - 选择两个具有不同限制的表

时间:2011-11-19 15:15:20

标签: php mysql sql greatest-n-per-group

我有两张这样的表:

om_manga:

id  | link      | manganame     | viewed    
---------------------------------------------
1   | naruto    | naruto        | 11215     
2   | bleach    | bleach        | 32442     
3   | one piece | one_piece     | 11215     
4   | gents     | gents         | 4334      

om_chapter:

id  | manganame     | chapter   | volume
-----------------------------------------
1   | naruto        | 1         | 1
2   | naruto        | 12        | 1
3   | naruto        | 22        | 1
4   | bleach        | 10        | 1
5   | bleach        | 15        | 1
6   | gents         | 1         | 1

我想要的是选择,例如按顺序查看om_manga的10行,并选择1行最后一行形式om_chapter,在两个表中使用相同的manganame ...

我在查询结果中想要的是:

id  | manganame     | viewed    | chapter   | volume
----------------------------------------------------
1   | naruto        | 14511     | 22        | 1
2   | bleach        | 45151     | 15        | 1

这是我的PHP代码:

$query  = mysql_query("SELECT `link`,`cover`,`manganame` FROM `om_manga` WHERE `Active`='1' ORDER BY `viewed` DESC LIMIT ".$options['number_of_popular_manga']); //
while($row = mysql_fetch_array($query)){ 
    $rww = mysql_fetch_array(mysql_query("SELECT `chapter`,`volume`,`manganame` FROM `om_chapter` WHERE `manganame`='".sql_quote($row['link'])."' AND `Active`='1' ORDER BY `etime` DESC LIMIT 1")); //
    if (isset($rww['chapter'])) $volchap = '<a href="read.php?manga='.$row['link'].'&amp;chap='.$rww['chapter'].'">Vol.'.$rww['volume'].' CH.'.$rww['chapter'].'</a>'; else $volchap = '';
    echo '<li class="index-right-ul-li"><img onerror="this.src=\'images/noimage.jpg\'" src="images/info/'.$row['cover'].'" width="30" height="33"/><div class="index-right-ul-div"><p class="index-right-ul-div-tital"><a href="manga.php?manga='.$row['link'].'">'.htmlspecialchars($row['manganame'], ENT_QUOTES).'</a></p><p class="index-right-ul-div-chapter">'.$volchap.'</p></div></li>';
}

我想将此代码更改为一个查询而不是两个查询..

我该怎么做?

5 个答案:

答案 0 :(得分:2)

SELECT id,viewed,manganame,
    (SELECT chapter
    FROM om_chapter 
    WHERE om_manga.manganame=om_chapter.manganame
    ORDER BY chapter DESC
    LIMIT 0,1) AS chapter,
    (SELECT volume
    FROM om_chapter 
    WHERE om_manga.manganame=om_chapter.manganame
    ORDER BY chapter DESC
    LIMIT 0,1) AS volume
FROM om_manga
ORDER BY viewed DESC
LIMIT 0,10

答案 1 :(得分:1)

SELECT om.link, om.cover, om.manganame, max_chapter.*
FROM om_manga om ,

(Select chapter, volume, manganame, Max(etime)
from om_chapter
group by chapter, volume, manganme) max_chapter

WHERE om.Active='1' And om.manganame = max_chapter.manganame
limit 10

我还没有测试过这个查询,但它的理念应该可行

答案 2 :(得分:0)

这是基本的SQL,将其调整为您的PHP代码:

select m.id, m.manganame, m.viewed, c.chapter, c.volume
from om_manga m
inner join om_chapter c on m.manganame = c.manganame
inner join (
    select manganame, max(chapter) chapter
    from om_chapter
) ss on c.manganame = ss.manganame and c.chapter = ss.chapter
order by m.viewed desc
limit 10

这是问题的变体。

为了进一步学习,这里有一个非常全面的答案:SQL Select only rows with Max Value on a Column

答案 3 :(得分:0)

首先,我建议不要使用字符串来关联表,而是使用用户的ID,所以:

om_manga:
id | name | viewed

om_chapter:
id | id_manga | chapter | volume

对于您的问题,使用您的结构组合表:

$query = "SELECT m.*, c.*, max(c.chapter) as last_chapter FROM om_manga m, om_chapter c WHERE m.manganame=c.manganame AND c.Active='1' ORDER BY c.etime DESC, m.viewed DESC LIMIT ".$options['number_of_popular_manga']);

使用我的结构:

$query = "SELECT m.*, c.*, max(c.chapter) as last_chapter FROM om_manga m, om_chapter c WHERE m.id=c.id_manga AND c.Active='1' ORDER BY c.etime DESC, m.viewed DESC LIMIT ".$options['number_of_popular_manga']);

答案 4 :(得分:0)

select m.`link`,m.`cover`,m.`manganame`, latest_chapter.chapter, latest_chapter.volume
from `om_manga` m
join (select c.manganame, max(c.volume) volume from om_chapter c) 
              as latest_volume
              on m.manganame = latest_volume.manganame
join (select c.manganame, c.volume, max(c.chapter) chapter from om_chapter c)
              as latest_chapter
              on m.manganame = latest_chapter.manganame 
                and latest_chapter.volume = latest_volume.volume
order by m.viewed desc
limit 10