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 8560235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:13:51+00:00 2026-06-11T16:13:51+00:00

I have the following SQL code; SELECT a.*, b.maxDate as last_payment_date, c.package as payment_for_package,

  • 0

I have the following SQL code;

SELECT  a.*, b.maxDate as last_payment_date, c.package as payment_for_package, (SELECT COUNT(id) FROM uploads WHERE user = a.id) AS upload_count
                FROM users a 
                    INNER JOIN payments c
                        ON a.id = c.user_id
                    INNER JOIN
                    (
                        SELECT user_id, MAX(date) maxDate, period
                        FROM payments
                        GROUP BY user_id
                    ) b ON c.user_id = b.user_id AND
                            c.date = b.maxDate

                WHERE a.package = 1
                AND b.maxDate < $twomonths
                AND (SELECT COUNT(id) FROM uploads WHERE user = a.id) > 10
                AND b.period = 1
                ORDER BY b.maxDate ASC
                LIMIT 50

As you can see I use the nested statement (SELECT COUNT(id) FROM uploads WHERE user = a.id) twice and I was wondering if there is a way to re-use this statement for improved performance? I’ve tried using the alias upload_count but you can’t use alias’ in WHERE clauses. Appreciate the help

  • 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-11T16:13:52+00:00Added an answer on June 11, 2026 at 4:13 pm

    Just join to the subquery and correlate the results:

    SELECT
        a.*,
        b.maxDate as last_payment_date,
        c.package as payment_for_package,
        upload_counts.upload_count
    
    FROM users a 
    
    INNER JOIN payments c
        ON a.id = c.user_id
    
    INNER JOIN
    (
        SELECT user_id, MAX(date) maxDate, period
        FROM payments
        GROUP BY user_id
    ) b ON c.user_id = b.user_id AND
           c.date = b.maxDate
    
    INNER JOIN
    (
        SELECT user, COUNT(id) AS upload_count
        FROM uploads
        GROUP BY user
    ) upload_counts ON upload_counts.user = a.id
    
    WHERE a.package = 1
    AND b.maxDate < $twomonths
    AND upload_counts.upload_count > 10
    AND b.period = 1
    
    ORDER BY b.maxDate ASC
    LIMIT 50;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL statementL: select DATE(bla), count(*) from tableA group by DATE(bla) UNION
I have the following SQL code: select val.PersonNo, val.event_time, clg.number_dialed from vicidial_agent_log val join
What is the difference between the following pieces of SQL code: select count(*) from
I have the following sql code: $statement = `maildb`; $query = SELECT COUNT(*) as
I have the following SQL code: SELECT eml AS Email Address FROM emailtbl WHERE
I have the following code: SELECT c.c_id, c_name, count(h.h_id) AS count FROM c LEFT
I have following SQL code: SELECT m.email FROM members as m WHERE exists (
I have the following SQL: select code, distance from places; The output is below:
I have the following SQL: select code, distance from places; The output is below:
I have the following SQL statement: SELECT efforts.user_id, project_tasks.task_name, sum(hours) FROM efforts, users, project_tasks

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.