gpt4 book ai didi

php - 为什么此查询重复此代码中的名字和姓氏

转载 作者:行者123 更新时间:2023-11-29 22:25:01 25 4
gpt4 key购买 nike

enter image description here

当我运行这个查询时,我得到了正确的结果,但名称和父亲的名字在所有行中重复。我只想要一次。例如,我输入 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";

}

?>

最佳答案

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

关于php - 为什么此查询重复此代码中的名字和姓氏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30351304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com