在我們學校,當學生好時,他們會得到一個存儲在數據庫中的虛擬磅(或美元)。Mysql參考子查詢導致父母在哪裏子句
這是我的查詢:
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來執行查詢因此:文本佔位符)。
當涉及到父查詢中的WHERE條件時,我遇到了一些問題。
凡說:
... AND (total_pounds>=:lowerPoundLimit and total_pounds<=:upperPoundLimit)
的total_pounds場是子查詢的列結果,但是當我運行查詢它不斷想出:
Unknown column 'total_pounds' in 'where clause'
有誰知道一個解決方案對此?
感謝
菲爾
非常感謝您的回覆,它的工作原理:) –
這兩個查詢與返回結果有任何區別嗎?再次感謝:) –
我認爲他們應該返回相同的結果,但缺乏數據我無法測試它們。 –