Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:52:06+00:00 2026-06-16T04:52:06+00:00

In our school, when a student is good, they get given a virtual pound

  • 0

In our school, when a student is good, they get given a virtual pound (or dollar) which is stored in a database.

This is my query:

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

(I’m using PHP PDO to perform the query hence the :text placeholders).

I’m having a bit of an issue when it comes to the WHERE condition in the parent query.

Where it says:

... AND (total_pounds>=:lowerPoundLimit and total_pounds<=:upperPoundLimit)

The total_pounds field is a column result of a subquery, however when I run the query it keeps coming up with:

Unknown column 'total_pounds' in 'where clause'

Does anyone know a solution to this?

Thanks

Phil

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:52:07+00:00Added an answer on June 16, 2026 at 4:52 am

    The problem is that the alias names is not yet available when the WHERE clause is evaluated. The problem should be solved by moving the subqueries from the SELECT clause into JOIN clauses like this:

    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
    

    It is also seems possible to write the query without subqueries:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our school's ERP has a nasty database structure to it and since it is
The following code was been given from our school as a sample for circular
I teach programming at secondary school: this is our current status and I would
In our school, it is common to build games as class projects in implementing
I'm a beginning programmer and the I&CS program at our school starts us off
I am writing a small program for our local high school (pro bono). The
A simple description of our architecture for the school project we are doing: -we
We'll soon have some high school kids here in our company for some training
I'm creating a contest sign-up page for our annual math competition at my school.
Our team is currently working on completely redesigning our school's website and one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.