Possible Duplicate:
how to select two columns from two tables in mysql
I am trying to execute this query but i got different output. I want to count row from one column(grade
) from table grading
and select column(level)
from other table info
.
for($i=1; $i<9; $i++)
{
$result = mysql_query("SELECT COUNT(grade),level FROM grading,info WHERE grade = $i");
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['level'];
echo "</td>";
echo "<td>";
echo $row['COUNT(grade)'];
echo "</td></tr>";
}
}
my database:
level grade
A 1
B 2
C 6
D 3
E 4
F 5
G 6
H 8
My output:
level grade
A 8
B 8
C 8
D 8
E 8
F 16
G 0
H 8
but its wrong i want to count how many students have grade 1,2,3,4,5,6
but output display something else.
I think, you must use JOIN http://en.wikipedia.org/wiki/Join_(SQL) instead of FROM table1, table2.
I'm not sure I understand what you want, but if I am correct in what I think you're looking for, try:
select grade, count(*) from students group by grade;
GROUP BY
condenses that column into only distinct values and allows you to return via count how many rows were "grouped" into that distinct value.
More info here: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html