Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL UNION 2 queries containing ORDER BYs

Tags:

sql

mysql

union

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();
like image 209
Paolo Broccardo Avatar asked Jun 18 '11 17:06

Paolo Broccardo


1 Answers

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.

like image 72
Abhay Avatar answered Sep 22 '22 00:09

Abhay