开发者

Can I make a query which orders things by two columns?

I have a hiking site. When peopl开发者_运维百科e search for hikes, they care about two issues:

  1. How close is it in distance from them, and
  2. When is the event

So sometimes I need to present the search results ordered first by date, but if the date is the same, then the hikes need to be ordered by distance. And vice versa.

Is that sort of thing possible in SQL?


Just append the order-by statements, one after another. Simplied I'm sure:

SELECT 
  Distance,
  EventTime,
FROM Table1
ORDER BY Distance DESC, EventTime ASC
  • Note, you can order-by descending and ascending values within the same query.


The ORDER BY clause is the last clause of a SELECT statement. It takes a list of columns, separated by commas. The list is processed left to right, so

ORDER BY distance, event_start

...will order by distance first. Where there are duplicate distances, those will be ordered based on the event_start column values. By default, the ordering is in ASCending order -- you need to specify DESC before the comma to reverse the logic. IE:

ORDER BY distance DESC, event_start

...will put the largest distances at the top of the result set.


select * from table order by field1, field2;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜