Welcome to WebmasterWorld Guest from 54.227.14.23

Forum Moderators: open

Message Too Old, No Replies

mySQL: Problem using UNION with SORT BY

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

New User

5+ Year Member

joined:Oct 4, 2010
posts: 2
votes: 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)

Junior Member

5+ Year Member

joined:Apr 15, 2009
posts:148
votes: 0


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

Moderator from US 

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

joined:Dec 10, 2005
posts:5628
votes: 48


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)

New User

5+ Year Member

joined:Oct 4, 2010
posts: 2
votes: 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)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Apr 30, 2007
posts:1394
votes: 0


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)

Moderator from US 

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

joined:Dec 10, 2005
posts:5628
votes: 48


Can parentheses ever be used?

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