Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2005
    Posts
    79

    Unanswered: 2 Order By's with 3 unions

    Hi,

    I have a query with 3 unions and I want to order the first two unions using one ORDER BY and the last union with a different ORDER BY. Unfortunately I am unable to group the first two selects with the first ORDER BY with parentheses without getting an error.

    ex:

    (SELECT a,b FROM table1)
    UNION
    (SELECT c,d FROM table2)
    ORDER BY a,d
    UNION
    SELECT x,y FROM table3
    ORDER BY y


    Thanks

  2. #2
    Join Date
    Apr 2005
    Location
    Baltimore, MD
    Posts
    297
    Try this. You need the "LIMIT" statement in there.
    Code:
    (
       SELECT * 
       FROM
       (
          (SELECT a,b FROM table1)
          UNION
          (SELECT c,d FROM table2)
       ) AS blah
       ORDER BY a,d
       LIMIT n
    )
    UNION ALL
    (
       SELECT x,y FROM table3
       ORDER BY y
       LIMIT n
    )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •