在mysql中使用外键连接两个表

时间:2016-02-26 09:37:10

标签: mysql join

我正在进行考勤计算页面。我有两张桌子......

manage_attendance

enter image description here

student_info

enter image description here

st_id是student_info的主键,也是manage_attendance的外键......

我必须加入两个表格,以便根据每个科目和学期显示每个学生的出勤率。

还有一个表格主题,我根据学期获得主题ID .....

请帮我这样做。

我想要这样......

 rollno  st_name   st_id subject sum(status) count(status) avg(status)

如果您想要澄清,请询问。

我的代码......

$re=mysql_query("SELECT  `id` 
FROM  `subjects` 
WHERE  `sem_id` =  '1'");
while($ro=mysql_fetch_row($re)){

//selecting subject id...

$res=mysql_query("SELECT b.roll_no ,b.st_name,a.st_id, a.subject, SUM( 
a.STATUS ) , COUNT( 
a.STATUS ) , AVG( 
a.STATUS ) 
FROM  student_info b INNER JOIN manage_attendance a ON a.st_id=b.st_id 
WHERE a.sem_id =  '1'
AND  a.subject ='".$ro[0]."'
GROUP BY  a.st_id ");

//innerjoin code

while($row=mysql_fetch_row($res)){
echo $row[0]."----|----".$row[1]."-----|----".$row[2]."-----|-----".$row[3]."-----|------".$row[4],"-----|-----".$row[5]."-----|------".$row[6];
echo"<br/>";
}


}

这是对的吗?

0 个答案:

没有答案
相关问题