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

时间:2022-10-25 22:18:18

I have two tables like this:

我有两个这样的表:

om_manga:

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:

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

What i want is to select, for example 10 rows form om_manga by order viewed and select 1 last row form om_chapter with equal manganame in two tables...

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

What i want in query result is this:

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

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

This is my PHP code:

这是我的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>';
}

I want to change this code in to one query instead two queries..

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

How can I do that?

我怎样才能做到这一点?

5 个解决方案

#1


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

#2


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

I have not tested the query, but its an idea that should work

我没有测试过这个查询,但它的一个想法应该可行

#3


0  

This is the basic SQL, adjust it to your PHP code:

这是基本的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

This is a variation of the problem.

这是每组最大问题的变体。

For further learning, there's a very comprehensive answer on the subject here: SQL Select only rows with Max Value on a Column

为了进一步学习,这里有一个非常全面的答案:SQL只选择列上具有最大值的行

#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

#5


0  

First I would recommend not using strings to relate tables but user their ID's, so:

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

om_manga:
id | name | viewed

om_chapter:
id | id_manga | chapter | volume

For your problem, combining the tables using your structure:

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

$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']);

And using my structure:

并使用我的结构:

$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']);

#1


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

#2


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

I have not tested the query, but its an idea that should work

我没有测试过这个查询,但它的一个想法应该可行

#3


0  

This is the basic SQL, adjust it to your PHP code:

这是基本的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

This is a variation of the problem.

这是每组最大问题的变体。

For further learning, there's a very comprehensive answer on the subject here: SQL Select only rows with Max Value on a Column

为了进一步学习,这里有一个非常全面的答案:SQL只选择列上具有最大值的行

#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

#5


0  

First I would recommend not using strings to relate tables but user their ID's, so:

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

om_manga:
id | name | viewed

om_chapter:
id | id_manga | chapter | volume

For your problem, combining the tables using your structure:

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

$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']);

And using my structure:

并使用我的结构:

$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']);