如何将涉及 MySQL 变量的复杂 MySQL 查询转换为 PDO 查询?
我的主要目标是通过连接多个表来获取某些列(学生:姓氏、名字、中间名;部门名称;以及当前年级),主要是获取“学生”表上的数据并通过外键获取其他必要的数据在其他表上(“g7/8/9/10_performance”和“sections”)。然后它们将被存储在数组等中。
MySQL 查询:
SET @grade_table = (SELECT CONCAT('g', student_currentGrade, '_performance') FROM students);
SET @stmt = CONCAT('SELECT students.student_last, students.student_first, students.student_middle, students.student_currentGrade, sections.section_name
FROM ((students LEFT JOIN (', @grade_table ,' INNER JOIN sections ON ', @grade_table,'.section_id = sections.section_id) ON ',@grade_table,'.student_id = students.student_id))');
PREPARE stmt1 FROM @stmt;
EXECUTE stmt1;
我使用 CONCAT() 函数来利用变量 @grade_table 作为学生数据库,很可能包含不同年级的学生。
尝试在类函数内进行 PDO 查询:
protected function getRecords() {
$sql =
"SET @grade_table = (SELECT CONCAT(\'g\', student_currentGrade, \'_performance\') FROM students);"
;
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql =
"SET @stmt = CONCAT(\'SELECT students.student_last, students.student_first, students.student_middle, students.student_currentGrade,
sections.section_name FROM ((students LEFT JOIN (\', @grade_table ,\' INNER JOIN sections ON \', @grade_table,\'.section_id = sections.section_id)
ON \',@grade_table,\'.student_id = students.student_id))\');"
;
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql = "PREPARE stmt1 FROM @stmt;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql = "EXECUTE stmt1;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
if($stmt->rowCount() == 0){
$stmt = null;
header('location: ../performances.php?error=not_found');
exit();
}
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
我尝试使用 XAMPP phpmyadmin 界面中的“create PHP”选项来帮助解决问题,因此 $sql 声明上有斜杠。但是,该函数不返回任何内容(在本例中,它将标头更改为“../performances.php?error=not_found”)。
调用上述函数的控制器类函数:
public function loadRecords(): array
{
$records = $this->getRecords();
$data = [];
while($row = $records){
$data[] = $row;
}
return $data;
}
我确实知道我编写的代码就像弗兰肯斯坦制作他的怪物一样,所以我会理解是否需要更好地组织数据库中的数据。
My main goal is to get certain columns (student: last, first, middle names; section name; and current grade level) from multiple tables by joining them, mostly getting data on the 'students' table and getting other necessary data through foreign keys on the other tables ('g7/8/9/10_performance', and 'sections'). They will then be stored in an array and such.
MySQL Query:
SET @grade_table = (SELECT CONCAT('g', student_currentGrade, '_performance') FROM students);
SET @stmt = CONCAT('SELECT students.student_last, students.student_first, students.student_middle, students.student_currentGrade, sections.section_name
FROM ((students LEFT JOIN (', @grade_table ,' INNER JOIN sections ON ', @grade_table,'.section_id = sections.section_id) ON ',@grade_table,'.student_id = students.student_id))');
PREPARE stmt1 FROM @stmt;
EXECUTE stmt1;
I used CONCAT() function to utilize the variable @grade_table as a student database will most likely contain students of differing grade levels.
Attempted PDO query inside a class function:
protected function getRecords() {
$sql =
"SET @grade_table = (SELECT CONCAT(\'g\', student_currentGrade, \'_performance\') FROM students);"
;
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql =
"SET @stmt = CONCAT(\'SELECT students.student_last, students.student_first, students.student_middle, students.student_currentGrade,
sections.section_name FROM ((students LEFT JOIN (\', @grade_table ,\' INNER JOIN sections ON \', @grade_table,\'.section_id = sections.section_id)
ON \',@grade_table,\'.student_id = students.student_id))\');"
;
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql = "PREPARE stmt1 FROM @stmt;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
$sql = "EXECUTE stmt1;";
$stmt = $this->connect()->prepare($sql);
$stmt->execute();
if($stmt->rowCount() == 0){
$stmt = null;
header('location: ../performances.php?error=not_found');
exit();
}
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
I tried using the "create PHP" option in the XAMPP phpmyadmin interface to maybe help things hence the slashes on the $sql declarations. But, the function returns nothing (in this case it changes the header to "../performances.php?error=not_found").
Controller Class function that calls the above function:
public function loadRecords(): array
{
$records = $this->getRecords();
$data = [];
while($row = $records){
$data[] = $row;
}
return $data;
}
I do understand that I made the code like how Frankenstein made his monster, so I'll understand if I need better organization of data in the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将您的 sql 重写为(但我也同意对您的问题的大多数评论
I would rewrite your sql to (but I also agree on most of the comments on your question ????):
This assumes that a student is only in one of the 'g7/8/9/10_performance' tables.