[英]Join query in Moodle only returns last column only while mysql returns correctly
我的心情數據庫中有2個表,例如equiz_details和equiz_responses,如下所示:
mdl_equiz_details:
mdl_equiz_details
我已使用聯接查詢以所需形式獲取結果,如下所示:
SELECT distinct ed.quizid,ed.questionnumber,ed.questiontext,ed.optiontext1,ed.optiontext2,ed.optiontext3,ed.optiontext4, ed.correctanswer,er.studentanswer FROM `mdl_equiz_details` as ed INNER JOIN `mdl_equiz_responses` as er ON ed.quizid = er.quizid and ed.questionnumber = er.questionnumber and er.studentid=4 and er.quizid=25
我在mysql數據庫中觸發了查詢,結果如下:
結果在mysql
我在我的心情頁面內使用相同的查詢,並嘗試獲取結果,如下所示:
<?php
require_once('../../config.php');
global $DB;
$quizresdetails = $DB->get_records_sql("SELECT ed.quizid,ed.questionnumber,ed.questiontext,ed.optiontext1,ed.optiontext2,ed.optiontext3,ed.optiontext4, ed.correctanswer,er.studentanswer FROM {equiz_details} ed INNER JOIN {equiz_responses} er ON ed.quizid = er.quizid and ed.questionnumber = er.questionnumber and er.studentid=4 and er.quizid=25");
var_dump($quizresdetails);
?>
我得到的結果如下:
Moodle頁面的結果
這顯然是最后的結果。
我做錯了什么? 如何在我的情人頁面中獲取完整結果?
Moodle版本:2.9.1
問題是您的查詢在每個記錄中返回其id相同的值:“ 25”。 Moodle的get_records_ *方法期望每個結果都有一個唯一的ID,因為它使用該ID作為返回結果數組中的數組鍵。 因此,它會查找所有結果,但是當它們遍歷它們時,會將每個結果添加到數組鍵“ 25”,因此,當到達最后時,它只會在返回的數組中包含最后一個結果。
因此,您需要為每行返回一個具有唯一ID的字段,即“ ID”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.