homepage Welcome to WebmasterWorld Guest from 54.167.174.90
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderator: open

Databases Forum

    
mySQL: Problem using UNION with SORT BY
joecap5



 
Msg#: 4210925 posted 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?

 

yaix2

5+ Year Member



 
Msg#: 4210925 posted 8:42 am on Oct 4, 2010 (gmt 0)

Isn't it "ORDER BY x DESC|ASC"?

LifeinAsia

WebmasterWorld Administrator lifeinasia us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4210925 posted 3:46 pm on Oct 4, 2010 (gmt 0)

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

joecap5



 
Msg#: 4210925 posted 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

enigma1

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4210925 posted 3:31 pm on Oct 5, 2010 (gmt 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)

LifeinAsia

WebmasterWorld Administrator lifeinasia us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4210925 posted 4:10 pm on Oct 5, 2010 (gmt 0)

Can parentheses ever be used?

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved