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

  • Home
  • SEARCH
  • 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 8688139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:17:02+00:00 2026-06-12T23:17:02+00:00

I have a table called Request. Other tables are linked to the Request table

  • 0

I have a table called Request.
Other tables are linked to the Request table through a request id.
There is a TwitterTweet table and a FacebookPost table.
So a single request can have 50 TwitterTweets and/or 20 FacebookPosts or any amount of Tweets/Posts
We can add them together for a total count of 70.

I’m trying to create a query that could tell me what is the request with the highest total count.

I know this is wrong:
(I attempted to just order them by the counts within the TwitterTweet, but it would not let me do an OUTER JOIN which I thought
would bring back the Count.count column. It forced me to do a Left Join for it to compile. My Logic was to do a join so
that the results were calculated for each row by the requestid)

SELECT r1.`id` AS requestid, r1 . * 
FROM  `Request` AS r1
LEFT JOIN 

(SELECT COUNT( * ) AS count, rid
FROM 

((SELECT  `TwitterTweet`.`id` AS  `smid` ,  `TwitterTweet`.`requestid` AS rid
FROM  `TwitterTweet` 
WHERE  `TwitterTweet`.`requestid` = requestid
AND  `TwitterTweet`.`active` =1) AS talias

)) AS Count ON ( Count.rid = requestid ) 
ORDER BY Count.count

*When I tried to add in the Facebook side it would not compile any more
(The concept is that the results are added from TwitterTweet with the results from FacebookPost
that are attached to the specific requestid which would give us a count. The entire result
set should be ordered by that count)

SELECT r1.`id` AS requestid, r1 . * 
FROM  `Request` AS r1
LEFT JOIN 

(SELECT COUNT( * ) AS count, rid
FROM 

((SELECT  `TwitterTweet`.`id` AS  `smid` ,  `TwitterTweet`.`requestid` AS rid
FROM  `TwitterTweet` 
WHERE  `TwitterTweet`.`requestid` = requestid
AND  `TwitterTweet`.`active` =1 ) AS talias 

UNION All

(SELECT `FacebookPost`.`id` AS  `smid`, `FacebookPost`.`requestid` AS rid
FROM  `FacebookPost`
WHERE  `FacebookPost`.`requestid` = requestid
AND  `FacebookPost`.`active` = 1) as falias 

)) AS Count ON ( Count.rid = requestid ) 
ORDER BY Count.count

I updated the Query with an attempt to add an alias:

SELECT rid, SUM(count) total_count

FROM 

(
(SELECT COUNT(*) AS count, r.rid
FROM   request r
       JOIN TwitterTweet tt
       ON r.id = tt.requestid
WHERE  tt.active = 1
GROUP BY r.rid) AS twitter

UNION ALL

(SELECT COUNT(*) AS count, r.rid
FROM   request r
       JOIN FacebookPost fp
       ON r.id = fp.requestid
WHERE  fp.active = 1
GROUP BY r.rid ) AS fbook
)

GROUP BY rid

ORDER BY SUM(count) DESC

I made another adjustment to give the middle subquery an alias, but now I only get one row returned with a zero in the rid column and 5686 in the total_count column…the 5686 might be all of the results.

SELECT counts.rid, SUM(count) total_count

FROM 

(
SELECT COUNT(*) AS count, r.requestid AS rid
FROM   request r
       JOIN TwitterTweet tt
       ON r.id = tt.requestid
WHERE  tt.active = 1
GROUP BY r.requestid

UNION ALL

SELECT COUNT(*) AS count, r.requestid AS rid
FROM   request r
       JOIN FacebookPost fp
       ON r.id = fp.requestid
WHERE  fp.active = 1
GROUP BY r.requestid
) AS counts

GROUP BY counts.rid

ORDER BY SUM(count) DESC

Got it!!!
Thanks for your help guys, I had to remove those joins on the request:

SELECT counts.rid, SUM(count) total_count

FROM 

(
SELECT COUNT(*) AS count, tt.requestid AS rid
FROM  TwitterTweet tt
WHERE  tt.active = 1
GROUP BY tt.requestid

UNION ALL

SELECT COUNT(*) AS count, fp.requestid AS rid
FROM   FacebookPost fp
WHERE  fp.active = 1
GROUP BY fp.requestid
) AS counts

GROUP BY counts.rid

ORDER BY SUM(count) DESC
  • 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-12T23:17:04+00:00Added an answer on June 12, 2026 at 11:17 pm
    SELECT id, SUM(count) total_count
    
    FROM 
    
    (
    SELECT COUNT(*) AS count, r.id
    FROM   request r
           JOIN TwitterTweet tt
           ON r.id = tt.requestid
    WHERE  tt.active = 1
    GROUP BY r.id
    
    UNION ALL
    
    SELECT COUNT(*) AS count, r.id
    FROM   request r
           JOIN FacebookPost fp
           ON r.id = fp.requestid
    WHERE  fp.active = 1
    GROUP BY r.id
    ) sub
    
    GROUP BY id
    
    ORDER BY SUM(count) DESC
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables called A and B . Table A contains request details
I have one table called datas in there I have id , start_date ,
I have a table called Request and the data looks like: Req_ID R1 R2
I have a table called Request and data will be entered by two types
I have table called stats . In am inserting yes or no in the
i have table called as Support, which have a field named Name and contains
I have table called Buttons. Buttons table i have column button_number . Table contain
I have table called posts in my DB. Each post has field called social_network
I have a table called 'MyTable' that looks like: ID | Item | Type
I have a table called States and i am displaying values of states in

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.