0

我在某种留言板上工作,除了一小部分外,我的一切都在运行,线程需要按其中最新帖子的日期/时间排序(标准论坛格式),我遇到了很多麻烦缠住我的头。

这是我正在使用的查询,我知道它不漂亮也不安全,一旦我学会了如何正确地做,我就会重新设计它们。

$sql = "SELECT Thread_ID, Thread_Title, Board_ID, Author FROM threads WHERE Board_ID='$Board_ID' LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

while ($row = mysql_fetch_assoc($result))
{

$Thread_ID = $row['Thread_ID']; 
$Thread_Title = $row['Thread_Title'];   
$Board_ID = $row['Board_ID'];       
$Author = $row['Author']; 

$getauthor = mysql_query("SELECT * FROM members WHERE Member_ID='$Author'");
while ($row = mysql_fetch_assoc($getauthor))
{
$Post_Author = $row['Post_As']; }


$postcount = mysql_query("SELECT Post_ID FROM posts WHERE Thread_ID='$Thread_ID'");                 
$Posts = mysql_num_rows($postcount);

$getlatest = mysql_query("SELECT * FROM posts WHERE Thread_ID='$Thread_ID' ORDER by Post_DateTime DESC LIMIT 1");
while ($row = mysql_fetch_assoc($getlatest))
{
$Post_DateTime = time_ago($row['Post_DateTime']);
$Member_ID = $row['Member_ID']; }

$getmember = mysql_query("SELECT * FROM members WHERE Member_ID='$Member_ID'");
while ($row = mysql_fetch_assoc($getmember))
{
$Post_As = $row['Post_As']; }   

所以我想要做的是通过 $getlatest 对 $sql 进行排序,我尝试在 $sql 上方添加另一个查询,该查询与 $getlatest 基本相同,然后按此排序 $sql,但可惜它只是破坏了一切。

我知道我必须创建一个变量来对 $sql 进行排序,但它的那个变量让我发疯。

任何帮助将不胜感激,谢谢。

请求的当前错误消息:

致命错误:SQL - 'on 子句'中的未知列'posts2.LatestPost' - SELECT threads.Thread_ID、threads.Thread_Title、threads.Board_ID、threads.Author、Sub1.LatestPost、Sub1.PostCount、members.Post_As、members2.Member_ID AS LastPostMemberID, members2.Post_As AS LastPostMemberPostAs FROM threads INNER JOIN (SELECT Thread_ID, MAX(posts.Post_DateTime) AS LatestPost, COUNT(*) AS PostCount FROM posts GROUP BY Thread_ID) Sub1 ON threads.Thread_ID = Sub1.Thread_ID INNER JOIN members ON threads.Author = members.Member_ID INNER JOIN posts posts2 ON posts2.Thread_ID = Sub1.Thread_ID AND posts2.LatestPost INNER JOIN members members2 ON members2.Member_ID = posts2.Member_ID WHERE threads.Board_ID='1' ORDER BY Sub1.LatestPost DESC LIMIT 0, 25 在 C:\wamp\www\forum\include\threads.php 第 86 行

4

1 回答 1

0

您应该能够在您的第一个查询中使用类似这样的方法:-

$sql = "SELECT threads.Thread_ID, threads.Thread_Title, threads.Board_ID, threads.Author, MAX(posts.Post_DateTime) AS LatestPost
        FROM threads 
        LEFT OUTER JOIN posts ON threads.Thread_ID = posts.Thread_ID
        WHERE threads.Board_ID='$Board_ID' 
        GROUP BY SELECT threads.Thread_ID, threads.Thread_Title, threads.Board_ID, threads.Author
        ORDER BY LatestPost DESC
        LIMIT $offset, $rowsperpage";

编辑

未测试以下内容,但查看您的选择,我认为您可以将它们组合成一个 SELECT。像这样的东西: -

$sql = "SELECT threads.Thread_ID, threads.Thread_Title, threads.Board_ID, threads.Author, Sub1.LatestPost, Sub1.PostCount, members.Post_As, members2.Member_ID AS LastPostMemberID, members2.Post_As AS LastPostMemberPostAs
        FROM threads 
        INNER JOIN (SELECT Thread_ID, MAX(posts.Post_DateTime) AS LatestPost, COUNT(*) AS PostCount FROM posts GROUP BY Thread_ID) Sub1
        ON threads.Thread_ID = Sub1.Thread_ID
        INNER JOIN members 
        ON threads.Author = members.Member_ID
        INNER JOIN posts posts2
        ON posts2.Thread_ID = Sub1.Thread_ID AND posts2.Post_DateTime = Sub1.LatestPost
        INNER JOIN members members2
        ON members2.Member_ID = posts2.Member_ID
        WHERE threads.Board_ID='$Board_ID' 
        ORDER BY Sub1.LatestPost DESC
        LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

while ($row = mysql_fetch_assoc($result))
{

    $Thread_ID = $row['Thread_ID']; 
    $Thread_Title = $row['Thread_Title'];   
    $Board_ID = $row['Board_ID'];       
    $Author = $row['Author']; 
    $Post_Author = $row['Post_As']; 
    $Posts = $row['PostCount']; 
    $Post_DateTime = time_ago($row['LatestPost']);
    $Member_ID = $row['LastPostMemberID']; 
    $Post_As = $row['LastPostMemberPostAs']; 
于 2013-07-07T20:41:09.437 回答