当我运行此查询时,我得到的结果是正确的,但名称和父名称在所有行中重复。我只想要一次。我举了1773身份证号来测试它。
<?
$query = mysql_query("
SELECT *
FROM students,grades,subjects
WHERE
students.id_students = grades.id_students AND
grades.id_subjects = subjects.id_subjects AND
students.id_students = 1773 ;
");
while ($row = mysql_fetch_assoc($query)
){
$id_students = $row['id_students'];
$firstname = $row['firstname'];
$surename = $row['surename'];
$father = $row['father'];
$marks = $row['marks'];
$class = $row['class'];
$name = $row['name'];
echo "$firstname" . "$surename";
echo "$marks" ." " . "$name" ;
echo "<br>";
echo "$class";
}
?>
答案 0 :(得分:0)
在FROM
子句中只使用一个表,而其他人使用JOIN
。
SELECT s.*,
g.*,
su.*
FROM students s
JOIN grades g
ON g.id_students = s.id_students
JOIN subjects su
ON su.id_subjects = g.id_subjects
WHERE s.id_students = 1773
答案 1 :(得分:0)
由于之前的答案将被删除,这应该是您所需要的。 $data
数组与从数据库获得的数组相同。
<?php
$data = array(
0 => array(
'id_students' => 1,
'firstname' => 'Fuad',
'surename' => 'Bro',
'father' => 'fathers name',
'marks' => 30,
'class' => 1,
'name' => 'itd'
),
1 => array(
'id_students' => 1,
'firstname' => 'Fuad',
'surename' => 'Bro',
'father' => 'fathers name',
'marks' => 120,
'class' => 1,
'name' => 'biography'
),
2 => array(
'id_students' => 1,
'firstname' => 'Fuad',
'surename' => 'Bro',
'father' => 'fathers name',
'marks' => 120,
'class' => 1,
'name' => 'informatic'
)
);
$last_name = '';
foreach ($data as $row) {
if ($row['firstname'] != $last_name) {
echo '<b>' . $row['firstname'] . ' ' . $row['surename'] . ' (father: ' . $row['father'] . ')</b><br>';
}
echo 'class: ' . $row['class'] . '<br>';
echo 'subject name: ' . $row['name'] . '<br>';
echo 'marks: ' . $row['marks'] . '<br><br>';
$last_name = $row['firstname'];
}
/*
output:
Fuad Bro (father: fathers name) -- name and father's name just once
class: 1
subject name: itd
marks: 30
class: 1
subject name: biography
marks: 120
class: 1
subject name: informatic
marks: 120
*/
?>