gpt4 book ai didi

php - SQL JOIN 查询不显示任何内容 - PHP

转载 作者:行者123 更新时间:2023-11-29 06:54:59 24 4
gpt4 key购买 nike

我正在执行一个 sql 查询,但它没有从表中返回任何数据。我不确定为什么,我已经从 INNER 更改为 LEFT 但没有运气。我已多次修改查询,但找不到问题。想知道为什么我没有显示任何内容吗?

TABLE STRUCTURE

TABLE CREATE QUERY

PHP

$query = ("SELECT  class.class_name, class.class_caption, class.class_credit_hours, class.class_description, faculty_fname, faculty_lname
FROM class
LEFT JOIN section
ON class.id = section.class_id
LEFT JOIN faculty
ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id
LEFT JOIN faculty_titles
ON faculty_titles.faculty_id = faculty.id
LEFT JOIN faculty_education
ON faculty_education.faculty_id = faculty.id
LEFT JOIN major_class_br
ON major_class_br.class_id = class.id
LEFT JOIN major_minor
ON major_class_br.major_minor_id = major_minor.id
LEFT JOIN sched_sect_br
ON sched_sect_br.section_id = section.id
LEFT JOIN schedule
ON schedule.id = sched_sect_br.schedule_id
LEFT JOIN semester
ON semester.id = schedule.semester_id
LEFT JOIN office_hours
ON schedule.id = office_hours.schedule_id AND faculty.id = office_hours.faculty_id
");
//execute query
$result = mysql_query($query);

if ($result){

$totalhours = 0;

while ($row = mysql_fetch_assoc( $result ))
{
print "<b>" . $row['class_name'] . "</b><br>";
print $row['class_caption'] . "<br>";
print $row ['class_credit_hours'] . "hrs. <br>";
print $row ['faculty_lname'] . "hrs. <br>";
print $row ['faculty_fname'] . "hrs. <br>";
print $row['class_description'] . "<br>";
print "------------------------------<br />";
$totalhours += $row['class_credit_hours'];
}
}

print "<p>Total hours for Major: " . $totalhours . ".</p>";

所需的显示:

Computer Programming I
CP1000
4
James Doe
This course offers introduction to programming.

更新:这里发现了问题,但我不确定原因

ON faculty.id = section.faculty_id OR faculty.id = office_hours.faculty_id

最佳答案

  • 复制 SQL,转到 MySQL 管理员,在那里执行 SQL 并查看它是否返回任何结果。

  • 删除所有的 JOIN 并再次执行,您至少应该看到一些东西

  • 一个一个添加JOIN

  • 尝试确定哪个 JOIN 导致没有返回任何行

  • 检查连接的左侧和右侧,确保确实有有效数据要连接

或者使用 OUTER JOIN

关于php - SQL JOIN 查询不显示任何内容 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13301791/

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