Welcome to WebmasterWorld Guest from 23.20.186.146

Forum Moderators: open

Message Too Old, No Replies

mySQL: Problem using UNION with SORT BY

     
5:11 am on Oct 4, 2010 (gmt 0)



I'm trying to get this query to work:

(SELECT A, B, D
FROM table1
UNION ALL
SELECT A, B, C
FROM table2)
SORT BY B


I get an error though.

It works when I take the SORT BY out:
SELECT A, B, D
FROM table1
UNION ALL
SELECT A, B, C
FROM table2


but I want the query results sorted.

Does anyone know what I'm doing wrong?
8:42 am on Oct 4, 2010 (gmt 0)

5+ Year Member



Isn't it "ORDER BY x DESC|ASC"?
3:46 pm on Oct 4, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Weclome [webmasterworld.com] to WebmasterWorld!

Your parens are causing the error. Try:
SELECT A, B, D
FROM table1
UNION ALL
SELECT A, B, C
FROM table2
SORT BY B
12:42 am on Oct 5, 2010 (gmt 0)



You're both right!
It is ORDER BY.
I thought the parentheses were the right thing to do though.
Can parentheses ever be used?

Thanks
3:31 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Can parentheses ever be used?

They could to define precedence for various operations but not to enclose the whole query

eg:
select a,b,c from table where a=1 and (b=2 or b=3)
4:10 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Can parentheses ever be used?

They can also be used to form an entity:
SELECT *
FROM MyTable
WHERE ID=(SELECT Max(ID) FROM MyTable2)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month