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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:04:04+00:00 2026-05-27T14:04:04+00:00

I’m trying to create a single query containing optional aggregate values from multiple tables

  • 0

I’m trying to create a single query containing optional aggregate values from multiple tables and having some difficulty. I’m going to try to simplify my problem to as few fields as possible.

Tables

applications

appid | pageid

contests

id | appid | winnerid

signups

id | contestid | firstname | lastname

referrals

id | signupid

tickets

id | signupid

Purpose of Query

I’m trying to combine the applications and contests tables based on the pageid parameter, join the signups table to get the winner when available, and then a count of all signups, referrals, and tickets or simply a zero value when none is available.

What I’ve got so far

SELECT t1.*, `Winner`.Name AS Winner, IFNULL(`srt`.Signups,0) AS Signups,
  IFNULL(`srt`.Referrals,0) AS Referrals,
  IFNULL(`srt`.Tickets,0) AS Tickets
FROM applications a, (contests c LEFT JOIN

  /* Join signups table to retrieve winner's first/last name */ 
  (SELECT id, contestid, CONCAT(firstname, ' ' , lastname) AS Name
   FROM signups) `Winner`
     ON c.winnerid = `Winner`.id
     AND c.contestid = `Winner`.contestid) LEFT JOIN

  /* Join signups, referrals, and tickets to retrieve counts */
  (SELECT s.signupid, COUNT(*) AS Signups, Referrals, Tickets
   FROM (signups s LEFT JOIN
    (SELECT r.signupid, COUNT(r.id) AS Referrals
     FROM signups s, referrals r
     WHERE s.signupid = r.signupid) `Referrals` 
       ON s.signupid = `Referrals`.signupid) LEFT JOIN
         (SELECT t.signupid, COUNT(t.id) AS Tickets
          FROM signups s, tickets t
          WHERE s.signupid = t.signupid) `Tickets` 
            ON s.signupid = `Tickets`.signupid) `srt` 
              ON signupid = `srt`.signupid

WHERE a.id = c.applicationid
AND a.pageid = @pageid
ORDER BY c.id IN (SELECT id FROM contests WHERE active = 1) desc, c.addeddate desc;

Problem

I’m positive this isn’t the most efficient way to do what I’m trying to do and regardless it doesn’t work. The values for the signups/referrals/tickets simply returns a count for every record in each of the tables. The winner join works fine, and if I limit it to just the signups, that works as well. The signups table is linked to the contests table, and then the referrals and tickets are related directly to the signups table. Any help on this complex query would be greatly appreciated.

  • 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-05-27T14:04:05+00:00Added an answer on May 27, 2026 at 2:04 pm

    Forgive me if I am misunderstanding, and also for my lack of knowledge of differences between MSSQL and mysql, but I would try something like this:

    SELECT 
        ( SELECT CONCAT(IFNULL(firstname,''), ' ' , IFNULL(lastname,'')) 
          FROM Signups 
          WHERE id = c.winnerid) as Winner,
        ( SELECT COUNT(*) 
          FROM Signups  
          WHERE contestid = c.id) as Signups,
        ( SELECT COUNT(*)
          FROM referrals r LEFT JOIN signups su ON r.signupid = su.id
          WHERE su.contestid = c.id) as Referrals, 
        ( SELECT COUNT(*)
          FROM tickets t LEFT JOIN signups su ON t.signupid = su.id
          WHERE su.contestid = c.id) as Tickets
    FROM
        Contests c 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am currently running into a problem where an element is coming back from

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.