Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you ORDER BY in a query using MINUS?

I want to ORDER BY the result of a MINUS query.

My first attempt doesn't work:

SELECT *
FROM Table1
MINUS
SELECT *
FROM table2
WHERE table2.foo = 'bar'
ORDER BY foo

How would you do it?

oops: I was doing ORDER BY table2.foo instead of just ORDER BY foo. Now it works.

like image 954
ladookie Avatar asked Oct 08 '11 16:10

ladookie


1 Answers

However, to answer your question, you can use a with query:

with tmp_minus as (
    SELECT *
    FROM Table1
    MINUS
    SELECT *
    FROM table2
    WHERE table2.foo = 'bar'
) 
select * from tmp_minus 
ORDER BY foo

You should also be able to do a subselect:

select * from (
    SELECT *
    FROM Table1
    MINUS
    SELECT *
    FROM table2
    WHERE table2.foo = 'bar'
) tmp_minus 
ORDER BY foo
like image 84
case nelson Avatar answered Sep 28 '22 07:09

case nelson