0

在我们学校,当一个学生表现良好时,他们会得到一个存储在数据库中的虚拟英镑(或美元)。

这是我的查询:

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'

有谁知道解决这个问题?

谢谢

菲尔

4

2 回答 2

3

WHERE问题是在评估子句时别名还不可用。应该通过将子查询从SELECT子句移动到如下子句来解决问题JOIN

SELECT s.chosen_name, s.chosen_surname, s.regId, s.admission_number, 
       total_pounds.pound_amount as total_pounds,
       available_pounds.pound_amount as available_pounds
FROM TBL_student s
LEFT JOIN
  (SELECT student_id, SUM(pounds_amount) AS pound_amount 
   FROM tbl_pounds_log
   GROUP BY student_id) AS total_pounds
ON total_pounds.student_id = s.admission_number 
LEFT JOIN
  (SELECT student_id, SUM(b.pounds_amount) AS pound_amount
   FROM tbl_pounds_log 
   WHERE b.staff_id=:staffId 
   AND b.action_timestamp>(UNIX_TIMESTAMP()-3600)
   GROUP BY student_id) as available_pounds  
ON available_pounds.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.pound_amount >= :lowerPoundLimit 
        AND total_pounds.pound_amount <= :upperPoundLimit) 
GROUP BY s.admission_number 
ORDER BY s.chosen_surname ASC, s.chosen_name ASC  
LIMIT 0,10

似乎也可以编写没有子查询的查询:

SELECT s.chosen_name, s.chosen_surname, s.regId, s.admission_number,
       SUM(tp.pounds_amount) AS total_pounds
       SUM(ap.pounds_amount) AS available pounds
FROM tbl_students s
LEFT JOIN tbl_pounds_log tp
ON tp.student_id = s.admission_number
LEFT JOIN tbl_pounds_log ap
ON ap.student_id = tp.student_id
AND ap.staff_id = tp.staff_id
AND ap.staff_id = :staffId
AND ap.action_timestamp = tp.action_timestamp
AND ap.action_timestamp > (UNIX_TIMESTAMP()-3600)
WHERE s.chosen_name LIKE :termOne AND s.chosen_surname LIKE :termTwo
OR s.chosen_name LIKE :termThree AND s.chosen_surname LIKE :termFour
GROUP BY s.admission_number, s.name, s.regId, s.admission_number
HAVING SUM(tp.pounds_amount) BETWEEN upperPoundLimit AND lowerPoundLimit
ORDER BY s.chosen_surname, s.chosen_name
LIMIT 0,10
于 2012-12-18T14:05:35.593 回答
1

尽量避免子查询可能是“真正的”解决方案(可以连接表和 GROUP BY 结果)

无论如何,由于您不能在 WHERE 子句中引用“total_pounds”,因此一个简单的解决方案是重复子查询。这很难看,但也许查询优化器和/或您的服务器的缓存(如果启用)将避免每个子查询执行 2 次...

于 2012-12-18T12:23:20.643 回答