简体   繁体   中英

Problems ordering the results of a SQL query when I have ties

My tables look like this:

 qotwQuestion1a QuestionId [primarykey] Question MemberId PostDate qotwVote1a QuestionId [primarykey] MemberId [primarykey] Vote1a qotwMember MemberId [primarykey] Name Password emailId 

The Sql query below sums the number of votes for each questionId (which has a postDate between the startofweek and endofweek date) and then displays it.

$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'];
        }
}

This query works fine, except for the "ORDER BY total DESC". The query gives the result, but does not orders the result by "total". But my issue is to get the questionId which has the maximum number of votes. if there is a tie between a few questionIds, i would need all of those questions.

Can someone help me with this

Best Zeeshan

Your code is structured in such a way that you will only get one result record back every time the query runs. The SQL ORDER BY clause does not apply to the PHP code calling it.

You'll need to restructure this so that the ORDER BY clause is actually doing something.

I would replace the whole thing with just one query:

$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'];
}

This assumes that Question and Name are unique by ID. If not, you'll probably want to break this up into two queries, using the IDs instead of names to look up information:

   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

Then look up member name and question based on the QuestionId.

Or you could make a really, really big query:

 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

The problem is that the second query always returns single row. You should combine both queries using the GROUP statement, something like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM