Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
help with SQL 'ORDER BY'
Old 09-21-2005, 04:58 PM help with SQL 'ORDER BY'
Novice Talker

Posts: 10
Trades: 0
I have a column called displayOrder in a table of articles. The default value is 0. I need to display the records I select from the table according to the displayOrder, with the records containing the default value (0) last. So 1,2,3,4,5...0,0,0,0 etc.

When I do 'SELECT * FROM tblArticles ORDER BY displayOrder' it gives me the records with displayOrder=0 first. Is there a way to fix this with a different select statement?

Or maybe I could set a different default value? I tried setting the default to NULL, but that returned things in the same order. I don't know what else to set the default to in order to get records with the default displayOrder to always appear last.

Any suggestions?
bluepajamas is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-21-2005, 05:05 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
ORDER BY displayorder DESC

W3Schools
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-21-2005, 05:07 PM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
I would change the default value. Of couse those come first becuase 0 is always first.
Change the default value to some obscenely high number that you will never reach, like 999999
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 09-21-2005, 05:46 PM
Novice Talker

Posts: 10
Trades: 0
Yeah, I wasn't expecting 0 to not come first, that's just what it had defaulted to and null didn't work either. But I guess setting the default really high will work.

I was thinking there might be something like: "SELECT * FROM tblArticles WHERE (NOT displayOrder=0 ORDER BY displayOrder) AND displayOrder=0" but your solution is probably simpler.

Thanks.
bluepajamas is offline
Reply With Quote
View Public Profile
 
Old 09-21-2005, 06:00 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
... ORDER BY displayorder WHERE displayorder > 0 ;
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-21-2005, 08:37 PM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
That is not valid syntax. You cannot have a 'where' clause after the 'order by' statement.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 09-22-2005, 03:17 AM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Yep
Blame lack of sleep and a copy & paste c**k up

PHP Code:
... WHERE displayorder 0 ORDER BY displayorder 
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-22-2005, 09:22 AM
Anacrusis's Avatar
Defies a Status

Posts: 2,099
Name: Adam
Location: Colchester CT
Trades: 0
But that will remove the records that have a displayorder of 0, I don't think he wants to do that, he just wants them to show at the end of the results list. I don't think there's a gracefull way of doing that.
Anacrusis is offline
Reply With Quote
View Public Profile
 
Old 09-23-2005, 04:03 AM
Skilled Talker

Posts: 62
Trades: 0
There's always a way to do it, if you want to hack at the MySQL all night and day...

(You might try using a mathematical function in the ORDER BY clause to make zero greater than any other number on the scale and sort in an ascending fashion ... but I don't have all night or day to put that together)

... or not
__________________

Please login or register to view this content. Registration is FREE

Last edited by danlefree; 09-23-2005 at 04:08 AM..
danlefree is offline
Reply With Quote
View Public Profile Visit danlefree's homepage!
 
Reply     « Reply to help with SQL 'ORDER BY'
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.47608 seconds with 12 queries