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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:56:54+00:00 2026-06-01T09:56:54+00:00

I have five tables that are fairly similar in structure but different enough that

  • 0

I have five tables that are fairly similar in structure but different enough that I wanted to have them as separate tables. Essentially, the tables record events in five different “categories” that have some common fields and some other unique fields.

It now appears I need to write a report in which records from all five tables are shown in descending order of the record creation timestamp (standard across all tables). All tables have the common fields I need, things like fName, lName, scorePercent, numCorrect, etc.

I can’t seem to figure out the best way to write a query to select records from all five tables and display them in one table with a column showing the table from which the record came and the values for the common fields, ordered descending by datetime.

Is there any easy way to do this? Do I have to do it with PHP? I am starting to question my original design.

Thanks.

  • 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-01T09:56:56+00:00Added an answer on June 1, 2026 at 9:56 am

    If the data is similar enough to be queried together, it’s probably similar enough to be stored together in one table with an extra column to record the type, however…

    select * from (
        select 'table1', timestamp_col, fName, lName, scorePercent, numCorrect
        from table1 where ...
        UNION ALL
        select 'table2', timestamp_col, fName, lName, scorePercent, numCorrect
        from table2 where ...
        UNION ALL
        select 'table3', timestamp_col, fName, lName, scorePercent, numCorrect
        from table3 where ...
        UNION ALL
        select 'table4', timestamp_col, fName, lName, scorePercent, numCorrect
        from table4 where ...
        UNION ALL
        select 'table5', timestamp_col, fName, lName, scorePercent, numCorrect
        from table5 where ..
    ) x
    order by timestamp_col desc
    

    You could use a single where statement outside the inner query, but it would perform poorly, because all the rows of all the tables would be unioned. This way, only the rows actually needed are unioned.

    Note that UNION ALL is a better choice than UNION, because UNION ALL doesn’t sort (with UNION the data would be sorted twice – once to remove duplicates and again for the order by).

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

Sidebar

Related Questions

I'm trying to get this to work... I have a five tables that Im
I have five tables and i want to get result out of them. Here
I have five separate services where each one corresponds to a different database table
I have five tables in my database. Members, items, comments, votes and countries. I
I have a MySQL database with a few (five to be precise) huge tables.
I have five activities/screens that I would like to be able to swipe between,
I have five tables: tab_template template_group group user_group user Tab_template's are organized into groups
I have a number of tables with text around them describing them. Something like
I have five tables: models : id, name, specification models_networks : id, model_id, network_id
I have written some SQL code to create five tables with several relations(foreign keys).

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.