我有联系时排序SQL查询结果的问题

时间:2009-07-20 18:54:52

标签: mysql

我的表格如下:

qotwQuestion1a
    QuestionId [primarykey]
    Question
    MemberId
    PostDate

qotwVote1a
    QuestionId [primarykey]
    MemberId [primarykey]
    Vote1a

qotwMember
    MemberId [primarykey]
    Name
    Password
    emailId

下面的Sql查询总结了每个questionId(在startofweek和endofweek日期之间有postDate)的投票数,然后显示它。

$result2 = mysql_query("    SELECT * FROM qotwMember, qotwQuestion1a
                    WHERE   qotwMember.MemberId=qotwQuestion1a.MemberId 
                    AND     PostDate>='".$startofweek."' AND PostDate<='".$endofweek."'
                    ORDER BY qotwQuestion1a.QuestionId DESC ");
while($row2 = mysql_fetch_array($result2))
{   //echo("testing");
    $result3= mysql_query ("SELECT SUM(Vote1a) AS total FROM qotwVote1a
                            WHERE QuestionId='".$row2['QuestionId']."'
                            ORDER BY total DESC ");
        while($row3 = mysql_fetch_array($result3))
        {
            echo $row2['Question'] . " " .$row2['Name'] . " " .$row3['total'];
        }
}

此查询正常工作,但“ORDER BY total DESC”除外。查询给出结果,但不按“total”排序结果。 但我的问题是得到具有最大票数的问题。如果几个问题之间存在联系,我将需要所有这些问题。

有人可以帮助我吗

最佳

Zeeshan

2 个答案:

答案 0 :(得分:3)

您的代码的结构使得每次查询运行时只会返回一条结果记录。 SQL ORDER BY子句不适用于调用它的PHP代码。

您需要对其进行重组,以便ORDER BY子句实际上正在执行某些操作。

我会用一个查询替换整个事情:

$result3= mysql_query ("
   SELECT qotwQuestion1a.Question, qotwMember.Name, SUM(qotwVote1a.Vote1a) AS total 
   FROM qotwMember
   INNER JOIN qotwQuestion1a
     ON qotwMember.MemberId = qotwQuestion1a.MemberId
   INNER JOIN qotwVote1a
     ON qotwVote1a.QuestionId = qotwQuestion1a.QuestionId
   WHERE PostDate>='".$startofweek."' 
   AND PostDate<='".$endofweek."'
   GROUP BY qotwQuestion1a.Question, qotwMember.Name
   ORDER BY total DESC
");
while($row3 = mysql_fetch_array($result3)) {
  echo $row3['Question'] . " " .$row3['Name'] . " " .$row3['total'];
}

这假设问题和名称是ID唯一的。如果没有,您可能希望将其分解为两个查询,使用ID而不是名称来查找信息:

   SELECT qotwQuestion1a.QuestionId, SUM(qotwVote1a.Vote1a) AS total 
   FROM qotwQuestion1a
   INNER JOIN qotwVote1a
     ON qotwVote1a.QuestionId = qotwQuestion1a.QuestionId
   WHERE PostDate>='".$startofweek."' 
   AND PostDate<='".$endofweek."'
   GROUP BY qotwQuestion1a.QuestionId
   ORDER BY total DESC

然后根据QuestionId查找会员名称和问题。

或者你可以做一个非常非常大的查询:

 SELECT qotwQuestion1a.Question, qotwMember.Name, SubQuery.total
 FROM (
   SELECT qotwQuestion1a.QuestionId, SUM(qotwVote1a.Vote1a) AS total 
   FROM qotwQuestion1a
   INNER JOIN qotwVote1a
     ON qotwVote1a.QuestionId = qotwQuestion1a.QuestionId
   WHERE PostDate>='".$startofweek."' 
   AND PostDate<='".$endofweek."'
   GROUP BY qotwQuestion1a.QuestionId
 ) SubQuery
 INNER JOIN qotwQuestion1a
   ON SubQuery.QuestionId = qotwQuestion1a.QuestionId
 INNER JOIN qotwMember
   ON qotwMember.MemberId = qotwQuestion1a.MemberId
 ORDER BY total DESC

答案 1 :(得分:0)

问题是第二个查询总是返回单行。您应该使用GROUP语句组合这两个查询,如下所示:

SELECT qotwQuestion1a.QuestionId, SUM(Vote1a) AS total
 FROM qotwMember, qotwQuestion1a, Vote1a
 WHERE qotwMember.MemberId=qotwQuestion1a.MemberId 
   AND qotwQuestion1a.QuestionId=Vote1a.QuestionId
   AND PostDate>='".$startofweek."' AND PostDate<='".$endofweek."'
 GROUP BY qotwQuestion1a.QuestionId
 ORDER BY total desc;