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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:43:12+00:00 2026-06-15T15:43:12+00:00

Lets say I have 4 tables A,B,C,D with records and datetime field in each

  • 0

Lets say I have 4 tables A,B,C,D with records and datetime field in each table.

Currently Im using UNION ALL and ORDER BY Date DESC.

The problem is if a table A has 10 records added today, and table B has 10 records added yesterday then the first 10 results are all from table A.

Is it possible to get records ordered by date, but mix them between all 4 tables?

First record will be the latest added from table A, then the latest added from table B, then C, then D. And cycle them like that?

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-15T15:43:14+00:00Added an answer on June 15, 2026 at 3:43 pm

    Unfortunately, MySQL doesn’t have a ranking function – but you can simulate it with variables. I suggest trying something like the following:

    SELECT * FROM 
    (select *, @rownum := @rownum + 1 AS rank, 'A' as tbl 
     from (SELECT * FROM (SELECT  A.* FROM A ORDER BY `date` desc) sa) a, 
          (SELECT @rownum := 0) r
     union all
     select *, @rownum2 := @rownum2 + 1 AS rank, 'B' as tbl 
     from (select * from (SELECT  B.* FROM B ORDER BY `date` desc) sb) b, 
          (SELECT @rownum2 := 0) r2
     union all
     select *, @rownum3 := @rownum3 + 1 AS rank, 'C' as tbl 
     from (select * from (SELECT  C.* FROM C ORDER BY `date` desc) sc) c, 
          (SELECT @rownum3 := 0) r3
     union all
     select *, @rownum4 := @rownum4 + 1 AS rank, 'D' as tbl 
     from (select * from (SELECT  D.* FROM D ORDER BY `date` desc) sd) d, 
          (SELECT @rownum4 := 0) r4
    ) s
    order by rank, tbl
    

    Alternatively, try something like:

    SELECT * FROM 
    (SELECT S.*, 
            @rownum := CASE WHEN @tblvar <> S.tbl THEN 0 ELSE @rownum+1 END AS rank, 
            @tblvar := S.tbl AS tblvar 
     FROM 
     (SELECT * FROM 
      (SELECT  A.*, 'A' as tbl FROM A
       UNION ALL
       SELECT  B.*, 'B' as tbl FROM B
       UNION ALL
       SELECT  C.*, 'C' as tbl FROM C
       UNION ALL
       SELECT  D.*, 'D' as tbl FROM D) U
      ORDER BY tbl, `date` desc) S,
     (SELECT @rownum := 0) R,
     (SELECT @tblvar := '!') T
    ) Q
    order by rank, tbl
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table Lets say TopicComments which records each comments from every user
I have two tables lets say employees and order, both table have millions of
Lets say I have data in two tables. In one I have Order ID
lets say, i have two tables, one for object records and one for activity
Lets say that I have a table with a timestamp column full of records
I am using MySQL ver 5.5.8. Lets say I have the table,entries, structure like
Lets say we have 5 tables Fact_2011 Fact_2010 Fact_2009 Fact_2008 Fact_2007 each of which
I have 2 tables. lets just say its records and numbers the records.number contains
Let's say i have a table in a database with 10k records. I dont
Lets say I have five tables named table1, table2 ... table5. I have already

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.