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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:38:12+00:00 2026-05-18T06:38:12+00:00

I have two tables called ‘events’ and ‘topics’ each table can have many comments.

  • 0

I have two tables called ‘events’ and ‘topics’ each table can have many comments.

What I need to do is list all the events and topics with the amount of comments for each row. I’ve managed to return all the topics, which works great but I don’t know how I can add the events table to the MySql. The comments and events table fields are listed below. Can anyone help me with this query?

Events:

  • ID
  • Event_Name

Comments:

  • post_id <– the releated id for either the events or topics table
  • table <– The table that the row belongs to so either topics or events

    SELECT 
      t.id, t.title, c.created_at, 
      IF(ISNULL(c.allComments), 0, c.allComments) AS totalComments
    FROM topics AS t
    LEFT OUTER JOIN (
        SELECT created_at, post_id, COUNT(*) AS allComments 
        FROM comments
        GROUP BY post_id
    ) AS c ON c.post_id = t.id
    ORDER BY tc.created_at DESC, c.allComments DESC
    
  • 1 1 Answer
  • 1 View
  • 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-18T06:38:13+00:00Added an answer on May 18, 2026 at 6:38 am

    Sounds like events and topics should be the same table.

    Still, I think we can do this with a UNION. Events and Topics have the same columns i hope? (Or at least the same important ones?)

    (SELECT c.table as event_or_topic, e.*, count(C.table), MAX(C.created_at) as latest_c
    FROM events E LEFT JOIN comments C on (C.post_id = E.id)
    WHERE C.table = 'Events' 
    GROUP BY C.post_id)
    UNION
    (SELECT c.table as event_or_topic, t.id*, count(C.table), MAX(C.created_at) as latest_c
    FROM topics T LEFT JOIN comments C on (C.post_id = E.id)
    WHERE C.table = 'Topics' 
    GROUP BY C.post_id)
    ORDER BY latest_c
    

    Notice that the ORDER BY applies to the whole UNION, not the individual SELECTs.

    The use of LEFT JOIN should allow those rows without Comments to still show. I think the problem is that we have parts of our select dependent on comments (ie – C.table, ordering on last comment, etc). The count should be fine – will just be zero if there are no comments.

    You might need to change the SELECT part slightly. I’d like to display C.table so you know whether a row is a topic or event, but im afraid it might screw up the count. Do you need anything from comments besides the count? You use some columns other than post_id and table in your query that you neglected to explain in your question.

    You still have columns I don’t know what they are, like Comment’s zoneTable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables, one called Events and one called Documents. Each table has
I have two tables called A and B . Table A contains request details
I Have a database called 'lms' with two tables loan and value, table loan
I have two tables. My first table is called as WORLD and this table
I have two tables called categories and topics. categories has columns category_id and category_title.
I have two tables called Reviews and Levels. CREATE TABLE [dbo].[Reviews]( [ReviewID] [int] IDENTITY(1,1)
I have two tables having 1 to 1 relationship. One table called Person and
I have two tables: one called Projects, and another called Documents (projects has many
I have two tables, one called calendars and the other one called events .
I have two tables, one called episodes, and one called score. The episode table

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.