Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding all marks and show their prize
    primarykey
    data
    text
    <p>This is my table</p> <pre><code>+------+-------+--------+ | NAME | MARKS | SCHOOL | +------+-------+--------+ | N1 | 80 | S1 | | N2 | 90 | S2 | | N3 | 75 | S1 | | N4 | 70 | S2 | | N5 | 55 | S1 | | N6 | 75 | S1 | | N7 | 70 | S1 | | N8 | 80 | S1 | | N9 | 60 | S1 | +------+-------+--------+ </code></pre> <p>Here I am trying to display Name, marks of the students of each School with Marks Descending order. Also, first Three highest marks should display as <strong>First, Second, Third.</strong></p> <p>So the finial output looks</p> <pre><code>+------+-------+--------+-------+ | NAME | MARKS | SCHOOL | PRIZE | +------+-------+--------+-------+ | N1 | 80 | S1 | First | | N8 | 80 | S1 | First | | N3 | 75 | S1 | Second| | N6 | 75 | S1 | Second| | N7 | 70 | S1 | Third | | N9 | 60 | S1 | | | N5 | 55 | S1 | | +------+-------+--------+-------+ </code></pre> <p>I am trying with following code, I have tried three different queries not getting my output</p> <pre><code>&lt;table width="100%" border="1"&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Marks&lt;/th&gt; &lt;th&gt;Prize&lt;/th&gt; &lt;/tr&gt; &lt;?php $result = mysql_query("SELECT * FROM myTable WHERE SCHOOL= 'S1' ORDER BY Marks Desc"); //$sql="SELECT * FROM myTable AS t1 INNER JOIN (SELECT DISTINCT(Marks) AS best_marks FROM myTable WHERE SCHOOL= 'S1' ORDER BY Marks DESC LIMIT 3) AS t2 ON t1.Marks = t2.best_marks ORDER BY Marks DESC, Name ASC"; /* $sql = "SELECT * FROM myTable WHERE SCHOOL= 'S1' AND LEFT JOIN (select Marks from myTable order by Marks desc limit 1) AS max1 ON myTable .Marks=max1.Marks LEFT JOIN (select Marks from myTable order by Marks desc limit 2,1) AS max2 ON myTable .Marks=max2.Marks LEFT JOIN (select Marks from myTable order by Marks desc limit 3,1) AS max3 ON myTable .Marks=max3.Marks; ORDER BY Marks Desc"; */ while ($row = mysql_fetch_array($result)) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo ucfirst($row['Name']);?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row['Marks']; ?&gt;&lt;/td&gt; &lt;td&gt; &lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload