我有3代表的受试者,学生,和关系
下面是样本数据PHP的MySQL joinget导致
students table id name 1 John 2 Doe 3 Jane subject table id subject 1 Math 2 Science 3 English relation table id student_id subject_id 1 1 1 2 1 3 3 2 1 4 1 2
与ID此时sudent = 1具有3科目。我怎样才能得到这样的结果?
student name is John subjects are: Math Science English
我已经得到了学生的价值观,我想要做的就是给用户显示他的主题。 我完全混淆了使用连接表。我是一名PHP新手,上周我开始学习。我的代码出现错误。请帮忙。
我Cuurent代码是这样的:
<?php //mysql connection $query = " SELECT *,* FROM relation, subject on subject_id = id WHERE student_id = $student_id "; $result = mysql_query($query); ?> Student name is <?php echo $name ?><br /> Subjects are:<br /> <?php while($row = mysql_fetch_array($result)) { echo $row["subject"]; } ?>
为什么要打扰两个查询?这正是SQL旨在通过JOIN处理的数据构造类型。 – 2010-09-02 17:44:44
@Marc,他说他是新手,所以我想给出一个简单的实现,注意它以*开始*简单的方法是* – aularon 2010-09-02 17:46:42