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

The Archive Base Latest Questions

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

Let’s say i have three tables: users, quizzes and questions. Each has an id

  • 0

Let’s say i have three tables: users, quizzes and questions. Each has an id (integer) field and a created_at (datetime) field. I want to be able to put all of these tables together, sort by created_at desc, and then get a ‘page’ of records (ie by manipulating offset) where a page is ten records for example. Ie, i want to get the ten most recently created records from either of those three tables.

My first thought would just be to do an outer join but this would result in a massive join table which really isn’t very efficient. It’s also fundamentally wrong – i don’t want to combinatorize all the records, i just want to put them all together into one list effectively.

I then tried just putting both tables into the from clause (for two tables just to test it out) and using comcat to order the tables:

select users.id, users.created_at, quizzes.id, quizzes.created_at, concat(users.created_at, quizzes.created_at) from users, quizzes order by concat(users.created_at, quizzes.created_at) desc limit 5;

But this doesn’t work either as, again, the two tables are combined and it takes a long time (10ish seconds) to do the query. Plus, concat isn’t the right approach i think.

Can anyone see what i’m trying to do and help me out? I feel like i’m not explaining it very well, monday morning syndrome perhaps. cheers, max

EDIT – here’s my solution, a tweaked version of Tomalak’s answer below. I’m using the updated_at field rather than created_at but it’s just another datetime field.

SELECT id, updated_at FROM ( 
  SELECT concat('user_',id) as id, updated_at FROM users 
  UNION SELECT concat('quiz_',id) as id, updated_at FROM quizzes 
  UNION SELECT concat('question_',id) as id, updated_at FROM questions ) 
  as everything ORDER BY updated_at DESC LIMIT 0, 10;

A problem i had with Tomalak’s answer was that you lose track of which table each record is from. So, above, i concatenate the table name to the start of the id, so you end up with something that looks like this:

+---------------+---------------------+
| id            | updated_at          |
+---------------+---------------------+
| user_1        | 2010-11-23 16:38:11 | 
| quiz_1306     | 2010-11-23 10:37:49 | 
| user_2        | 2010-11-22 11:57:08 | 
| user_401      | 2010-11-22 11:48:20 | 
| quiz_1303     | 2010-11-22 11:47:53 | 
| question_5163 | 2010-11-22 11:42:20 | 
| question_5162 | 2010-11-22 11:42:16 | 
| question_5161 | 2010-11-22 11:37:11 | 
| question_5160 | 2010-11-22 11:34:55 | 
| quiz_1304     | 2010-11-22 11:31:54 | 
+---------------+---------------------+
10 rows in set (0.16 sec)
  • 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-18T06:05:53+00:00Added an answer on May 18, 2026 at 6:05 am

    Like this?

    SELECT 
      tbl, id, updated_at 
    FROM
      (
        SELECT 'users' AS tbl, id, updated_at FROM users
      UNION
        SELECT 'quizzes',      id, updated_at FROM quizzes
      UNION
        SELECT 'questions',    id, updated_at FROM questions
      ) as everything
    ORDER BY
      updated_at DESC
    LIMIT 10;
    

    In terms of query efficiency, it might be that this variant runs faster:

    SELECT 
      tbl, id, updated_at 
    FROM
      (
        SELECT   'users' AS tbl, id, updated_at 
        FROM     users 
        ORDER BY updated_at DESC LIMIT 10
      UNION
        SELECT   'quizzes',      id, updated_at 
        FROM     quizzes
        ORDER BY updated_at DESC LIMIT 10
      UNION
        SELECT  'questions',     id, updated_at 
        FROM     questions 
        ORDER BY updated_at DESC LIMIT 10
      ) as everything
    ORDER BY
      updated_at DESC
    LIMIT 10;
    

    You should have an index on (updated_at, id) on each individual table.

    This way the query can be fulfilled entirely from looking at the index, which is already sorted and also contains the id field. The original table would not have to be looked at, unless you want to add more fields to the output. Additionally, the “outer” ORDER BY (which can’t use an index) would only have to sort 30 rows.

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

Sidebar

Related Questions

Let's say I have a class with 10 methods, each method has different parameters.
Let's say I have this MySQL table: OK.. see the type field? Type 0
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say I have two assemblies: BusinessLogic and Web. BusinessLogic has an application setting
Let's say I have two tables: Member ------------------------ | user_id | first_name | ------------------------
Let's say I have data string like this... one=1&two=2&three=3&four=4&two=2 I'm using php foreach to
Let's say I have integer variables x and y, and I want an array
Let's say R is a mxn random matrix. How can generate R with each
Let's say i have this block of code, <div id=id1> This is some text
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):

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.