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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:32:25+00:00 2026-05-23T05:32:25+00:00

I am trying to UNION two queries which both contain ORDER BY’s. As I

  • 0

I am trying to UNION two queries which both contain ORDER BY’s.
As I have discovered, you can not order by queries that are part of a UNION.
I just don’t know how else to do this query then. Let me explain what I’m trying to do.

  1. I am trying to select the 40 most recent profiles and from that list select a random set of 20. I then want to union that with:
  2. Select 40 random profiles where the profile does not fall in the original 40 most recent profiles queried in the first set
  3. Randomly order that whole set of 60 records.

I am aware of the efficiency ramifications of using the Rand() function

SELECT profileId
  FROM (SELECT profileId
          FROM profile profile2
         WHERE profile2.profilePublishDate <= Now()
      ORDER BY profile2.profilePublishDate DESC
         LIMIT 0,40) AS profile1
ORDER BY RAND()
   LIMIT 0,20
UNION (SELECT profileId
         FROM profile profile4
        WHERE profileId NOT IN (SELECT profileId
                                  FROM profile profile4
                                 WHERE profile4.profilePublishDate <= Now()
                              ORDER BY profile4.profilePublishDate DESC
                                 LIMIT 0,40)    
     ORDER BY RAND()    
        LIMIT 0,40) as profile3
ORDER BY RAND()

UPDATE: This is the solution based on Abhay’s help below (thanks Abhay):

SELECT *
FROM
(
    (
        SELECT profileId
        FROM 
        (
            SELECT profileId
            FROM profile profile2
            WHERE profile2.profilePublishDate <= Now()
            ORDER BY profile2.profilePublishDate DESC
            LIMIT 0,40
        ) AS profile1
        ORDER BY RAND()
        LIMIT 0,20
    )
    UNION
    (
        SELECT profileId
        FROM profile profile4
        WHERE profileId NOT IN (
            SELECT * FROM
            (
            SELECT profileId
            FROM profile profile4
            WHERE profile4.profilePublishDate <= Now()
            ORDER BY profile4.profilePublishDate DESC
            LIMIT 0,40
            ) AS temp2
         )
        ORDER BY RAND()    
        LIMIT 0,40
    )
) TEMP
ORDER BY RAND();
  • 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-23T05:32:26+00:00Added an answer on May 23, 2026 at 5:32 am

    This is your solution:

    SELECT *
    FROM
    (
        **(**
            SELECT profileId
            FROM 
            (
                SELECT profileId
                FROM profile profile2
                WHERE profile2.profilePublishDate <= Now()
                ORDER BY profile2.profilePublishDate DESC
                LIMIT 0,40
            ) AS profile1
            ORDER BY RAND()
            LIMIT 0,20
        **)**
        UNION
        (
            SELECT profileId
            FROM profile profile4
            WHERE profileId NOT IN (
                SELECT profileId
                FROM profile profile4
                WHERE profile4.profilePublishDate <= Now()
                ORDER BY profile4.profilePublishDate DESC
                LIMIT 0,40
                )
            ORDER BY RAND()    
            LIMIT 0,40
        )
    ) TEMP
    ORDER BY RAND();
    

    The changes that I’ve made are:

    1. each of your queries that are part of UNION should be encased in brackets (shown in bold for the first query; the second is already encased)
    2. removed the alias profile3 for your 2nd query
    3. for the final ORDER BY RAND(), you must create the UNION resultset to a derived table; I have given it TEMP as the alias

    I haven’t tested the above query but I hope it should work. Let me know your findings.

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

Sidebar

Related Questions

I have a query that performs a UNION between two tables. I'm trying to
I'm trying to UNION the results of two queries. But I'm getting the following
I'm trying to SUM two rows obtained from differente SQL queries in UNION, is
I'm trying to write a SQL UNION that works on both MySQL and SQLite.
I am trying to find the union of two sets with map. I have
Trying to make a make generic select control that I can dynamically add elements
I'm trying to get a UNION ALL working with a CTE which I'm using
I have two SQL queries producing different results when I would expect them to
I have a UIScrollView which contains any number of thumbnails that should detect the
I have two related qestions on the code included below 1) I am trying

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.