Database Reference
In-Depth Information
Sorting results
In SQL, we can never be sure of the order in which the data is retrieved, unless
we explicitly sort the data. Some implementations of the retrieving engine may
show results in the same order as the one in which data was entered, or according
to a primary key. However, a sure way to get results in the order we want is by
sorting them explicitly.
When browsing results are displayed, any column header can be clicked to sort
on this column, even if it is not part of an index. Let's click on the
author_id
column header.
We can confirm that the sorting has occurred, by watching the SQL query at the top
of screen; it contains an
ORDER BY
clause.
We now see a small red triangle pointing upwards beside the
author_id
header.
This means that the current sort order is 'ascending'. Moving the mouse cursor over
the
author_id
header makes the red triangle change direction, to indicate what will
happen if we click on the header—a sort by descending
author_id
.
Another way to sort is by key. The
Sort
dialog shows all the keys already defined.
Here we see a key named
PRIMARY
—the name given to our primary key on the
isbn
field when we checked
Primary
for this field at creation time:
This might be the only way to sort on multiple fields at once (for multi-fields
indexes).
The default initial sort order is defined in
$cfg['Order']
with
ASC
for ascending,
DESC
for descending. The sort order can also be or
SMART
, which means that fields of
type
DATE
,
TIME
,
DATETIME
, and
TIMESTAMP
would be sorted in descending order,
whereas other field types will be sorted in ascending order.
Search WWH ::
Custom Search