0
$quiztotal = mysql_query('SELECT id,username,qname,quizpercentage FROM quiz');

echo '<table><tr><th>USERNAME</th>
                 <th>QUIZ NAME</th>
                 <th>PERCENTAGE</th>
                 <th>GRADE</th>
                 <th>POINTS</th></tr>';
while($row = mysql_fetch_array($quiztotal))
{
    $username = $row['username'];
    $qtotal = $row['quizpercentage'];
    $defaultsetting = mysql_query('SELECT id,letter,percentage,points FROM default');
    while($row = mysql_fetch_array($defaultsetting))
    {
        $letter = $row['letter'];
        $percentage = $row['percentage'];
        $points = $row['points'];
        echo '<tr><td>'.$username.'</td>
                  <td>'.qtotal.'</td>';
                  echo 'td>';
                    if(qtotal == $percentage)
                    {
                      echo $letter;
                    }
                  echo '</td>';
                  echo '<td>';
                  if($letter)
                  {
                     echo $points;
                  }
                  echo '</td></tr>';
    }
}

クイズテーブル

id    username   quizpercentage
1        35          66%
2        47          78%
3        53          90%     
4        56          96%
5        4           45%

デフォルト テーブル

   id     letter   percentage     points
   1        A         >=85         4.00
   2        B         80-84        3.67
   3        C         83-75        2.65
   4        D         74-55        2.00 

ここで >=85 は 85% から 100% までは何も始まりません。グレードは A/B/C/D です。ポイントも表示する必要があります。次のような出力が必要です。

  USERNAME      QUIZ NAME         PERCENTAGE      GRADE   POINTS
   35           TestSeries          85%            A       4.00
   47           TestSeries          70%            C       2.65
   53           preliminary         62%            D       2.00
4

3 に答える 3

3

You can't do it easily with the table structures you have right now, you'll need some changes. First one would be changing Quiz table so that field QuizPercentage contains a number (without the "%" sign). This should be done anyway because formatting belongs to the web page, not the database.

Next step would be adding two columns to table default (is that really the name? If yes, it's not a good name either). The two new columns would be the following: - PercentageFrom int - PercentageTo int

They will contain, respectively, the minimum and the maximum value of each score range. That means your table will look something like this:

  id     letter   percentage     points  PercentageFrom  PercentageTo
   1        A         >=85         4.00        85           100
   2        B         80-84        3.67        84            84
   3        C         83-75        2.65        75            83
   4        D         74-55        2.00        55            74

Now you would simply have to join Quiz table with the Scores table and get the data you need.

SELECT
  Q.id
  ,Q.username
  ,Q.qname
  ,Q.quizpercentage
  ,S.Letter
  ,S.Points
FROM
  Quiz Q
  JOIN
  Scores S ON
    (Q.quizpercentage BETWEEN S.PercentageFrom AND PercentageTo)
于 2012-08-18T17:53:11.843 に答える
2

あなたのデフォルトは人間だけが読むことができます。それは次のようになります:

id     letter   percentage_from percentage_to     points
1        A         85           100               4.00
2        B         80           84                3.67
3        C         75           79                2.65
4        D         55           74                2.00 
5        E         0            54                2.00 

次に、クエリは次のようになります。

$quiztotal 
= mysql_query(
'SELECT quiz.id, quiz.username,quiz.qname, default.letter, default.points
 FROM quiz, default 
 where quiz.quizpercentage <= defaut.percentage_to
 and quiz.quizpercentage  >= defaut.percentage_from');

必要なすべての情報を手に入れることができます

于 2012-08-18T17:49:41.463 に答える
0

CASE構文を確認してください: http://dev.mysql.com/doc/refman/5.0/en/case-statement.html

于 2012-08-18T17:41:38.900 に答える