使用另一个查询的结果对查询结果进行排序

时间:2013-07-07 20:24:36

标签: php mysql

我正在处理某种类型的留言板,除了一小部分外,我还有一个正在运行的东西,线程需要按照最新帖子的日期/时间排序(标准论坛格式),我有缠绕我的头很麻烦。

这是我使用的查询,我知道它不漂亮且不安全,一旦我学会了如何正确地做,我将重新修改它们。

$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成员ON threads.Author = members.Member_ID INNER JOIN发布posts2 ON posts2.Thread_ID = Sub1.Thread_ID AND posts2.LatestPost INNER JOIN members member2 ON members2.Member_ID = posts2.Member_ID WHERE threads.Board_ID ='1'ORDER BY Sub1。 LatestPost DESC LIMIT 0,25在第86行的C:\ wamp \ www \ forum \ include \ threads.php

1 个答案:

答案 0 :(得分: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'];