在我们学校,当一个学生表现良好时,他们会得到一个存储在数据库中的虚拟英镑(或美元)。
这是我的查询:
SELECT s.chosen_name, s.chosen_surname, s.regId, s.admission_number,
(
SELECT SUM(a.pounds_amount)
FROM tbl_pounds_log a
WHERE a.student_id=l.student_id
) AS total_pounds,
(
SELECT SUM(b.pounds_amount)
FROM tbl_pounds_log b
WHERE b.student_id=l.student_id
AND b.staff_id=:staffId
AND b.action_timestamp>(UNIX_TIMESTAMP()-3600)
) AS available_pounds
FROM TBL_student s
LEFT JOIN tbl_pounds_log l
ON l.student_id=s.admission_number
WHERE ((s.chosen_name LIKE :termOne AND s.chosen_surname LIKE :termTwo)
OR (s.chosen_name LIKE :termThree AND s.chosen_surname LIKE :termFour))
AND (total_pounds>=:lowerPoundLimit
AND total_pounds<=:upperPoundLimit)
GROUP BY s.admission_number
ORDER BY s.chosen_surname ASC, s.chosen_name ASC
LIMIT 0,10
(我使用 PHP PDO 执行查询,因此使用 :text 占位符)。
当涉及到父查询中的 WHERE 条件时,我遇到了一些问题。
它在哪里说:
... AND (total_pounds>=:lowerPoundLimit and total_pounds<=:upperPoundLimit)
total_pounds 字段是子查询的列结果,但是当我运行查询时,它不断出现:
Unknown column 'total_pounds' in 'where clause'
有谁知道解决这个问题?
谢谢
菲尔