get_records_sql returns 在 moodle 中使用内部连接只有一个结果
get_records_sql returns only one result using inner join in moodle
我使用以下代码在 MOODLE 中获取类别和相应的课程详细信息
global $DB;
$catlist = $DB->get_records_sql(
'SELECT ca.id,ca.name,ca.coursecount, c.id as course_id , c.category,
c.fullname, c.shortname , c.summary , c.format, c.startdate , c.timecreated
FROM {course_categories} as ca inner join {course} as c on ca.id = c.category
WHERE ca.parent > ? and ca.visible = ? and c.visible = ? ', array('0','1','1'));
echo "<pre>";print_r($catlist); echo "</pre>";
当我执行此查询时,我得到的结果数组只有一个结果行,而在 mysql 数据库中直接执行相同的 sql returns 多行。
Table course_categories 有 2 个类别 'account' 和 'business' 具有使用可见 =1 的活动条件并且还包含父类别。
Table 课程有 4 门课程,每门课程 2 都与类别 'account' 和 'business'
相关
这样的结果:
Array
(
[1] => stdClass Object
(
[id] => 1
[name] => Accounts
[coursecount] => 2
[course_id] => 4
[category] => 1
[fullname] => Finance
[shortname] => Finance
[summary] =>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
[format] => weeks
[startdate] => 1461695400
[timecreated] => 1461653620
)
[2] => stdClass Object
(
[id] => 2
[name] => Business
[coursecount] => 2
[course_id] => 5
[category] => 2
[fullname] => Animal Health Honours (BSc(Hons))
[shortname] => Animal Health Honours
[summary] =>
Sl/NO, Course Name, Duration. HARDWARE & NETWORKING. 1, Advacnce Diploma in Computer Hardware Maintanance & Networking(ADCHMN), 12 Months.
[format] => weeks
[startdate] => 1461781800
[timecreated] => 1461760598
)
)
任何人都可以帮助解决这个问题。
在 Moodle 中调用任何 get_records* 函数的结果将作为一个数组返回,该数组由结果中的第一个字段索引(这非常有用,例如,如果您得到一个用户数组记录,然后想直接跳到一条记录,根据用户id)。
由于您的查询 returns categoryid 作为第一个字段,每个类别只会返回 1 个结果(如果您将 debugging 设置为 developer,您将收到有关此的警告) .
要修复,要么使用不同的字段作为返回的第一个值(在这种情况下,c.id 是一个很好的选择),要么使用 get_recordset* 函数之一,使用 foreach 遍历结果。
我使用以下代码在 MOODLE 中获取类别和相应的课程详细信息
global $DB;
$catlist = $DB->get_records_sql(
'SELECT ca.id,ca.name,ca.coursecount, c.id as course_id , c.category,
c.fullname, c.shortname , c.summary , c.format, c.startdate , c.timecreated
FROM {course_categories} as ca inner join {course} as c on ca.id = c.category
WHERE ca.parent > ? and ca.visible = ? and c.visible = ? ', array('0','1','1'));
echo "<pre>";print_r($catlist); echo "</pre>";
当我执行此查询时,我得到的结果数组只有一个结果行,而在 mysql 数据库中直接执行相同的 sql returns 多行。
Table course_categories 有 2 个类别 'account' 和 'business' 具有使用可见 =1 的活动条件并且还包含父类别。 Table 课程有 4 门课程,每门课程 2 都与类别 'account' 和 'business'
相关这样的结果:
Array
(
[1] => stdClass Object
(
[id] => 1
[name] => Accounts
[coursecount] => 2
[course_id] => 4
[category] => 1
[fullname] => Finance
[shortname] => Finance
[summary] =>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
[format] => weeks
[startdate] => 1461695400
[timecreated] => 1461653620
)
[2] => stdClass Object
(
[id] => 2
[name] => Business
[coursecount] => 2
[course_id] => 5
[category] => 2
[fullname] => Animal Health Honours (BSc(Hons))
[shortname] => Animal Health Honours
[summary] =>
Sl/NO, Course Name, Duration. HARDWARE & NETWORKING. 1, Advacnce Diploma in Computer Hardware Maintanance & Networking(ADCHMN), 12 Months.
[format] => weeks
[startdate] => 1461781800
[timecreated] => 1461760598
)
)
任何人都可以帮助解决这个问题。
在 Moodle 中调用任何 get_records* 函数的结果将作为一个数组返回,该数组由结果中的第一个字段索引(这非常有用,例如,如果您得到一个用户数组记录,然后想直接跳到一条记录,根据用户id)。
由于您的查询 returns categoryid 作为第一个字段,每个类别只会返回 1 个结果(如果您将 debugging 设置为 developer,您将收到有关此的警告) .
要修复,要么使用不同的字段作为返回的第一个值(在这种情况下,c.id 是一个很好的选择),要么使用 get_recordset* 函数之一,使用 foreach 遍历结果。