我在我的第5页查询中,如果可能的话,我希望只有一个查询,我不知道是否有一个更好的策略,我正在做什么或它的确定。
这是我加载图书的主要查询
$mess = $mysqli->prepare('SELECT * from ( SELECT m.id, cm.voteup,
cm.votedown,cr.book_descr FROM books m
INNER JOIN books_vote cm ON cm.bookid = m.id
INNER JOIN book_info cr ON cr.bookid = m.id
WHERE bookid = ? ORDER BY m.date DESC LIMIT 30)ddd
ORDER BY m.date ASC');
$mess->bind_param("i", $book);
$mess->execute();
$mess->store_result();
$mess->bind_result($id,$voteup,$votedown);
while($row = $mess->fetch()){
// im fetching here in this <div class='areabooks'>
}
然后我重置查询以获取其他具有IF条件的div
$mess->data_seek(0);
while($row = $mess->fetch()){
if($voteup - $votedown >= 5){
//I fetch here in this <div class='areabooks2'>
}
}
然后我使用此查询检查顶部
MAX(voteup - votedown )
$messtop = $mysqli->prepare('SELECT ..........
INNER JOIN ...
INNER JOIN ( select id ,MAX(voteup - votedown ) as maxe
from books
where voteup - votedown >= 5
group by id ) tt
WHERE bookid = ?
ORDER BY maxe DESC,cm.votedown asc,cm.voteup DESC
limit 1');
$messtop->bind_param("i", $book);
$messtop->execute();
$messtop->store_result();
$messtop->bind_result($id,$voteup,$votedown);
$messtop->fetch();
// then i fetch in this <div class='topbook' >
然后我再次重置第一个查询以获取其他具有IF条件的div
$mess->data_seek(0);
while($row = $mess->fetch()){
if($votedown - $voteup >= 5){
//I fetch here in this <div class='areabooks3' >
}
}
然后我使用此查询获取顶部
MAX(votedown - voteup )
$messtop = $mysqli->prepare('SELECT ..........
INNER JOIN ...
INNER JOIN ( select id ,MAX(votedown - voteup) as maxe
from books where votedown - voteup >= 5
group by id ) tt
WHERE bookid = ?
ORDER BY maxe DESC,cm.voteup ASC,cm.votedown DESC
limit 1');
$messdown->bind_param("i", $book);
$messdown->execute();
$messdown->store_result();
$messdown->bind_result($id,$voteup,$votedown);
$messdown->fetch();
// then i fetch in this <div class='topbottonbook' >
我正在寻找的是,如果有一个策略只使用一个查询并在所有这些div中获取它,那些div就像那样分开。
<first div>
<second div>
<3rd div>
<4th div>
5th div
如何简化此感谢?
答案 0 :(得分:0)
我怀疑3个查询是否可以组合成一个。
首先查询:
推迟JOIN
,直到您获得30个'最新'bookids
。也就是说,将JOIN移到外部SELECT
。
也有INDEX(id, date)
。但我很困惑。通常id
是PRIMARY KEY
,因此是唯一的。但查询暗示一本书有多个日期。是什么赋予了? (请提供SHOW CREATE TABLE
。)
mysql命令行工具(或Workbench或phpmyadmin)是你的朋友。使用其中一个手动运行查询。我想你会发现错误。请修理。