Mysql Reference子查询结果在parent where子句中

时间:2012-12-18 12:18:44

标签: mysql subquery

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

这是我的疑问:

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'

有没有人知道这方面的解决方案?

由于

菲尔

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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