我有以下查询。当我直接在MySQL中运行查询时,会获得所有信息,但是当我在PHP脚本中运行相同的查询时,它将丢失一些信息。我首先在MySQL中运行查询,以确保获得所需的一切,然后将查询复制到我的php脚本中。谁能帮忙吗?
$get_attendance_profile_stmt = $conn->prepare('SELECT
p_info.fname AS \'First name\',
p_info.lname AS \'Lastname\',
stud_info.t_belt_id,
t_b.short_descr,
t_b.long_descr,
stud_info.h_belt_id,
h_b.short_descr,
h_b.long_descr,
AB.*,
ROUND(
(
AB.Considered_Attended / AB.Classes_Tought * 100
),
0
) AS \'Attendance %\'
FROM
(
SELECT
student_id,
SUM(
CASE WHEN attendance_reason = 1 THEN 1 ELSE 0
END
) AS \'Sick\',
SUM(
CASE WHEN attendance_reason = 2 THEN 1 ELSE 0
END
) AS \'Injured\',
SUM(
CASE WHEN attendance_reason = 3 THEN 1 ELSE 0
END
) AS \'Work\',
SUM(
CASE WHEN attendance_reason = 4 THEN 1 ELSE 0
END
) AS \'School\',
SUM(
CASE WHEN attendance_reason = 5 THEN 1 ELSE 0
END
) AS \'Did not attend\',
SUM(
CASE WHEN attendance_reason = 6 THEN 1 ELSE 0
END
) AS \'Present\',
SUM(
CASE WHEN attendance_reason != 5 THEN 1 ELSE 0
END
) AS \'Considered_Attended\',
MAX(DateCounter) AS \'Classes_Tought\'
FROM
attendance a
JOIN(
SELECT COUNT(
DISTINCT CONVERT(creation_date, DATE)
) AS DateCounter
FROM
attendance
WHERE
creation_date <= (SELECT DATE_SUB(NOW(), INTERVAL -6 MONTH))
) DT
WHERE
creation_date <= (SELECT DATE_SUB(NOW(), INTERVAL -1 MONTH))
GROUP BY
student_id
) AB
JOIN student_info stud_info ON
stud_info.student_id = AB.student_id
JOIN users u ON
stud_info.uid = u.uid
JOIN personal_info p_info ON
u.uid = p_info.uid
LEFT JOIN t_belts t_b ON
stud_info.t_belt_id = t_b.t_belt_id
LEFT JOIN h_belts h_b ON
stud_info.h_belt_id = h_b.h_belt_id
WHERE
u.account_status = \'Active\'');
任何帮助将不胜感激。
答案 0 :(得分:0)
PHP数组需要具有唯一索引。您的查询返回2个具有相同名称的列,因此第二个值就是您所获得的值(它们在赋值过程中覆盖了第一个值,简单的示例https://3v4l.org/sbs41)。从屏幕截图中可以看到,第二列为空。在第一列(或第二列)上使用别名,以便您在PHP中的索引是唯一的。例如
SELECT
p_info.fname AS \'First name\',
p_info.lname AS \'Lastname\',
stud_info.t_belt_id,
t_b.short_descr,
t_b.long_descr,
stud_info.h_belt_id,
h_b.short_descr as short_descr_dontcare,
h_b.long_descr as long_descr_dontcare
或
SELECT
p_info.fname AS \'First name\',
p_info.lname AS \'Lastname\',
stud_info.t_belt_id,
t_b.short_descr as short_descr_real,
t_b.long_descr as long_descr_real,
stud_info.h_belt_id,
h_b.short_descr,
h_b.long_descr
使用后一种方法,您需要使用_real
修改PHP数组中的索引。