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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:50:54+00:00 2026-05-18T22:50:54+00:00

I need to combine various MySQL tables for a common result. The needed and

  • 0

I need to combine various MySQL tables for a common result. The needed and expected result is a list of last X comments on a website but the tables are individuals, like: news_comments, pages_comments, etc. Individually, this tables has JOINS to users database.

The structure of 3 of this comments table are:

news_comments, pages_comments, other_comments
id int ai
id_new/id_page/id_other int
id_user int
comment text
created int (timestamp)

The table for users:

id int ai
login int
password varchar
...

I don’t complete this last because we only need id and login.

The normal use of this queries (individuals) are:

SELECT users.id, users.login, news_comments.*, news_comments.id AS id_comment
FROM users, news_comments
WHERE users.id=news_comments.id_user AND news_comments.id_new=$id
ORDER BY created DESC
LIMIT $offset,$rows

And works fine!

But in the backend i want to have a list of all the comments on the website to moderate and control it. I need to combine all the comments tables, order it by created desc and limit the result to make a pagination.

I use MySQL and PHP, so if I need to use PHP there is no problem with that.

How I can do it?

Thanks in advance!

  • 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-18T22:50:54+00:00Added an answer on May 18, 2026 at 10:50 pm
    SELECT  *
    FROM    (
            SELECT  *
            FROM    (
                    SELECT  *
                    FROM    news_comments
                    WHERE   id_new = $id
                    ORDER BY
                            created DESC
                    LIMIT $rows
                    ) q
            UNION ALL
            SELECT  *
            FROM    (
                    SELECT  *
                    FROM    pages_comments
                    WHERE   id_new = $id
                    ORDER BY
                            created DESC
                    LIMIT $rows
                    ) q
            UNION ALL
            SELECT  *
            FROM    (
                    SELECT  *
                    FROM    other_comments
                    WHERE   id_new = $id
                    ORDER BY
                            created DESC
                    LIMIT $rows
                    ) q
            ) q
    JOIN    users u
    ON      u.id = q.id_user
    ORDER BY
            created DESC
    LIMIT   $offset, $rows
    

    Note that limits are applied to each of the inner queries as well so that they could benefit from the indexes.

    Create the following indexes:

    CREATE INDEX ix_newscomments_new_created ON news_comments (id_new, created)
    CREATE INDEX ix_pagescomments_new_created ON pages_comments (id_new, created)
    CREATE INDEX ix_othercomments_new_created ON other_comments (id_new, created)
    

    for this to work fast

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

Sidebar

Related Questions

I have a MySql database with three tables I need to combine in a
I need to combine to a string column and the result of a row_number()
I have two lists that i need to combine where the second list has
I need to combine one Header Table and six Detail Tables into a single
I need to combine/merge two php functions, but I can't get the syntax right.
I have two tables with data and need to combine them and order by
I need to combine the price with USD but I am getting this error.
I have two lists List that I need to combine in third list and
I need to combine lines in two files, basing in the condition, that in
I need to combine my main query with a subquery: Current query (which works):

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.