Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
658 views
in Technique[技术] by (71.8m points)

sql - make zero appear last in a list of ascending numbers

I'm using SQLite in an Android application.
In all of my tables I have a default row with an index of 0 that holds default values for that table.
In most situations the default number for each field is 0 and that is the best number to use.
However, when I sort my data using an ORDER BY statement I want to have all of my zero value fields put at the end of the list as they're generally empty and using default information.
Excluding these fields in a where statement is unacceptable as I'm trying to sort information, not filter it.

Here's what I have so far:

select distinct item.name, epoch_date.epoch
from epoch_date, time
where (time.begin_date_id = epoch_date._id)
and (item.time_id = time._id)
order by epoch_date.epoch;

In my sandbox database, this returns something like:

"item 1", 0
"item 2", 0
"item 4", 0
.
.
.
"item 3",  1275350400
"item 42", 1275472800
"item 12", 1275472800

But what I want is:

"item 3",  1275350400
"item 42", 1275472800
"item 12", 1275476400
.
.
.
"item 1", 0
"item 2", 0
"item 4", 0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Equivalent to the CASE statement suggestion, but shorter:

 ORDER BY epoch_date.epoch == 0, epoch_date.epoch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...