我必须显示学生的“约翰”信息。
约翰的信息表-学生
id | Roll | name | class | year
==================================
1 | 1 | john | 7 | 2
约翰的马克表-马克
id | std_id | sub_id | th | pr
===========================================
1 | 1 | 1 | 60 | 20
2 | 1 | 2 | 55 | 18
约翰的教育年表-年
id | title
=============
1 | 2017
2 | 2018
约翰的课堂表-课堂
id | title
=============
7 | Seven
8 | Eight
约翰的主题表-主题
id | title
=============
1 | Science
2 | Math
现在我的要求如下
Name: John
Class: Seven
Year: 2018
Science: 60 (TH)
Math: 55 (TH)
但是,这里得到重复的结果。我得到john的信息,该信息重复计算了该学生的mark
表中的总行。
Name: John
Class: Seven
Year: 2018
Science: 60 (TH)
Name: John
Class: Seven
Year: 2018
Math: 55 (TH)
我尝试使用GROUP BY
到std.id,以防止重复,但它仅显示标记表的第一行。在这里,只有科学标记显示出来。
$result=$con->prepare(
"SELECT
student.id, student.en_name AS name, student.class,
class.title AS class,
year.title AS year,
subject.title AS sub,
mark.sub_id,
mark.th,
mark.pr
FROM student
JOIN year ON year.id = student.year
JOIN class ON class.id = student.class
JOIN mark ON mark.std_id = student.id
INNER JOIN subject ON subject.id = mark.sub_id
WHERE student.id=:id;"
) or die($con->error);
$result->bindParam(':id',$_POST['std']);
$result->execute();
while($row=$result->fetch(PDO::FETCH_ASSOC)){
$name=$row['name'];
$class=$row['class'];
$sub=$row['sub'];
$year=$row['year'];
$th=$row['th'];
$pr=$row['pr']; echo"
<article id='blg_half'>
<article class='left'>
Name: $name<br/>
Class: $class<br/>
Year: $year<br/>
$sub : $th(th)
</article>
</article>";
}
答案 0 :(得分:0)
尝试使用内部联接,无需根据您的问题进行分组
SELECT
student.roll, student.name, student.address, student.img, student.class, student.section,
class.title AS class,
section.title AS section,
year.title AS year,
mark.id, mark.th, mark.pr
FROM student
inner JOIN year ON year.id = student.year
inner class ON class.id = student.class
inner JOIN section ON section.id = student.section
inner JOIN mark ON mark.std_id = student.id
group by student.roll, student.name, student.address, student.img, student.class, student.section,
class.title AS class,
section.title AS section,
year.title AS year,
mark.id, mark.th, mark.pr
答案 1 :(得分:0)
根据您更新的问题,您实际上需要GROUP_CONCAT才能产生结果:
SELECT
student.id,
student.roll, student.name, student.address, student.img,
class.title AS class,
subject.title AS sub,
`year`.title AS yr,
GROUP_CONCAT(mark.th) AS th,
GROUP_CONCAT(mark.pr) AS pr
FROM student
JOIN `year` ON `year`.id = student.`year`
JOIN class ON class.id = student.class
LEFT JOIN mark ON mark.std_id = student.id
LEFT JOIN subject ON subject.id = mark.sub_id
WHERE student.id = :id
GROUP BY student.id
MySQL应该可以使用上述SQL,但是标准SQL应该按SELECT中除GROUP_CONCAT()之外的所有字段进行分组
此外,当选择mark.id时,您不能按...分组-这毫无意义
答案 2 :(得分:0)
尝试与标记表左连接:
SELECT
student.roll, student.name, student.address, student.img, student.class, student.section,
class.title AS class,
section.title AS section,
year.title AS year,
mark.id, mark.th, mark.pr
FROM mark
LEFT JOIN student ON student.id = mark.std_id
LEFT JOIN year ON year.id = student.year
LEFT JOIN class ON class.id = student.class
LEFT JOIN section ON section.id = student.section
WHERE mark.std_id =:id
答案 3 :(得分:0)
尝试一下
SELECT
student.id, student.name,
GROUP_CONCAT(CONCAT(subject.title,year.title,class.title,mark.th)) as Marks
FROM student
LEFT JOIN year ON year.id = student.year
LEFT JOIN class ON class.id = student.class
LEFT JOIN mark ON mark.std_id = student.id
LEFT JOIN subject ON subject.id = mark.sub_id
WHERE student.id=:id
GROUP BY student.id