我有一个mysql查询:
SELECT
mdl_user_info_data.data,
mdl_user.firstname,
mdl_user.lastname,
mdl_grade_grades.itemid,
mdl_grade_items.itemname,
mdl_course.cpd,
mdl_grade_grades.timecreated AS DATE
FROM mdl_user
INNER JOIN mdl_grade_grades ON mdl_user.id = mdl_grade_grades.userid
INNER JOIN mdl_grade_items ON mdl_grade_grades.itemid = mdl_grade_items.courseid
INNER JOIN mdl_course ON mdl_grade_items.courseid = mdl_course.id
INNER JOIN mdl_user_info_data ON mdl_user.id = mdl_user_info_data.userid
WHERE mdl_grade_grades.timecreated >= '1370091991'
AND mdl_grade_grades.timecreated <= '1370524243'
AND mdl_user_info_data.fieldid = 1
查询正确执行,并且在 phpmyadmin 中正确显示。有4条记录。其中一些记录具有相同的用户名,例如:
09118514 Elazabi Gillyman 108 Career Management1 1370508988
09118514 Elzaabi Gillyman 108 Career Management2 1370508988
03228812 Magria Martinia 171 NULL 4 1370294859
03228812 Magria Martinia 171 Making Budgeting 1370294859
但是当我尝试在 php 中使用它时,只有 2 条记录:
09118514 Elazabi Gillyman 108 Career Management2 1370508988
03228812 Magria Martinia 171 Making Budgeting 1370294859
我假设以某种方式将具有相同 id 的记录分组,但我需要查看所有结果。我究竟做错了什么?
这是确切的查询:
$sql = "SELECT
mdl_user_info_data.data,
mdl_user.firstname,
mdl_user.lastname,
mdl_grade_grades.itemid,
mdl_grade_items.itemname,
mdl_course.cpd,
mdl_grade_grades.timecreated AS DATE
FROM mdl_user
INNER JOIN mdl_grade_grades ON mdl_user.id = mdl_grade_grades.userid
INNER JOIN mdl_grade_items ON mdl_grade_grades.itemid = mdl_grade_items.courseid
INNER JOIN mdl_course ON mdl_grade_items.courseid = mdl_course.id
INNER JOIN mdl_user_info_data ON mdl_user.id = mdl_user_info_data.userid
WHERE mdl_grade_grades.timecreated >= '1370091991'
AND mdl_grade_grades.timecreated <= '1370524243'
AND mdl_user_info_data.fieldid = 1";
$users = $DB->get_records_sql($sql);
var_dump($users);
var_dump 包含这个结果(请注意,为了便于阅读,我已经编辑了一些字段):
array(5) {
[20038617]=> object(stdClass)#387 (7) { ["data"]=> string(8) "20038617" ["firstname"]=> string(9) "Bhekumuzi" ["lastname"]=> string(7) "Shongwe" ["itemid"]=> string(2) "72" ["itemname"]=> string(21) "Delegating Assessment" ["cpd"]=> string(4) "2.00" ["date"]=> string(10) "1370518594" }
["03429262"]=> object(stdClass)#388 (7) { ["data"]=> string(8) "03429262" ["firstname"]=> string(7) "Shirlee" ["lastname"]=> string(5) "Levey" ["itemid"]=> string(3) "100" ["itemname"]=> string(27) "The IFRS Agenda part 2 quiz" ["cpd"]=> string(4) "0.25" ["date"]=> string(10) "1370431311" }
[10001873]=> object(stdClass)#386 (7) { ["data"]=> string(8) "10001873" ["firstname"]=> string(6) "Bertha" ["lastname"]=> string(11) "Muchineripi" ["itemid"]=> string(3) "103" ["itemname"]=> string(45) "State of the nation analysis 2012 part 3 quiz" ["cpd"]=> string(4) "0.25" ["date"]=> string(10) "1370255012" }
["09118514"]=> object(stdClass)#385 (7) { ["data"]=> string(8) "09118514" ["firstname"]=> string(6) "Elzabi" ["lastname"]=> string(7) "Gillman" ["itemid"]=> string(3) "108" ["itemname"]=> string(17) "Career Management" ["cpd"]=> string(4) "2.00" ["date"]=> string(10) "1370508988" }
["03228812"]=> object(stdClass)#383 (7) { ["data"]=> string(8) "03228812" ["firstname"]=> string(5) "Maria" ["lastname"]=> string(6) "Martin" ["itemid"]=> string(3) "172" ["itemname"]=> string(90) "Managerial Megatrends and Financial Control Quiz - pass the quiz for automatic CPD logging" ["cpd"]=> string(1) "4" ["date"]=> string(10) "1370297204" } }