我需要做什么
“章节”表
“pages”表
如何构建一个返回页面关联数组的查询,以便我有如下内容(注意:如果需要,我可以使用php进行一系列查询):
谢谢!
答案 0 :(得分:0)
我认为您正在寻找这样的查询:
SELECT
pages.id AS page_id,
pages.name AS page_name,
chapters.name AS page_chapter
FROM pages
INNER JOIN chapters ON (chapters.id = pages.chapter_id)
WHERE pages_id IN
(page_id -3, page_id - 2, page_id - 1, page_id,
page_id + 1, page_id + 2, page_id + 3)
ORDER BY page_id ASC
答案 1 :(得分:0)
如果您可以更改表格,为什么不在页面表中添加排序列?
而不是使用int
并且每次插入新页面时都必须移动所有内容
也可以使用double
,只使用此页和下一页之间的中间值。
答案 2 :(得分:0)
$cChapter = 5; //Current chapter's id
$cPage = 4; //Current page's id
//$PagePos = the current's page position (when 1 is the first page , not 0)
//$MaxPosExists = the highest position value for the current chapter
$range = 3; //3 pages before and 3 pages after
$minPosition = $PagePos - $range;
$maxPosition = $PagePos + $range;
if($minPosition <= 0)
{
$minPosition = 1; //the current page is the first,second or third page.
}
if($maxPosition > $MaxPosExists)
{
$maxPosition = $MaxPosExists;
}
$getPages = mysql_query("SELECT * FROM pages WHERE chapter_id='$cChapter' AND position BETWEEN $minPosition AND $maxPosition ORDER BY position ASC");
现在,只需获取该查询并确保忽略该数组中的当前页面。