如何将涉及 MySQL 变量的复杂 MySQL 查询转换为 PDO 查询?

发布于 2025-01-11 05:27:13 字数 2285 浏览 0 评论 0原文

我的主要目标是通过连接多个表来获取某些列(学生:姓氏、名字、中间名;部门名称;以及当前年级),主要是获取“学生”表上的数据并通过外键获取其他必要的数据在其他表上(“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2025-01-18 05:27:13

我会将您的 sql 重写为(但我也同意对您的问题的大多数评论

I would rewrite your sql to (but I also agree on most of the comments on your question ????):

SELECT 
    students.student_last, 
    students.student_first, 
    students.student_middle, 
    students.student_currentGrade, 
    sections.section_name
FROM students
LEFT JOIN g7_performance g7 ON g7.student_id = students.student_id
                           AND students.student_currentGrade=7
LEFT JOIN g8_performance g8 ON g8.student_id = students.student_id
                           AND students.student_currentGrade=8
LEFT JOIN g9_performance g9 ON g9.student_id = students.student_id
                           AND students.student_currentGrade=9
LEFT JOIN g10_performance g10 ON g10.student_id = students.student_id
                           AND students.student_currentGrade=10
INNER JOIN sections ON sections.section_id = COALESCE(g10.section_id, g9.section_id, g8.section_id, g7.section_id) ;

This assumes that a student is only in one of the 'g7/8/9/10_performance' tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文