order by 语句

来源:互联网 发布:淘宝学校排名 编辑:程序博客网 时间:2024/06/10 12:08
A=> select * from Websites where alexa>15 and (country='CN' or country='USA') order by alexa;
 id |         name         |                        url                         | alexa |  country   
----+----------------------+----------------------------------------------------+-------+------------
  4 | WeiBo                | http://www.weibo.com/                              |    20 | CN        
  3 | CaiNiao              | http://www.runoob.com/                             |  4689 | CN        
(2 rows)


A=> select * from Websites where alexa>15 and (country='CN' or country='USA') order by id;
 id |         name         |                        url                         | alexa |  country   
----+----------------------+----------------------------------------------------+-------+------------
  3 | CaiNiao              | http://www.runoob.com/                             |  4689 | CN        
  4 | WeiBo                | http://www.weibo.com/                              |    20 | CN        

(2 rows)




A=> select * from Websites order by country,alexa;
 id |         name         |                        url                         | alexa |  country   
----+----------------------+----------------------------------------------------+-------+------------
  2 | TaoBao               | https://www.taobao.com/                            |    13 | CN        
  4 | WeiBo                | http://www.weibo.com/                              |    20 | CN        
  3 | CaiNiao              | http://www.runoob.com/                             |  4689 | CN        
  1 | Google               | https://www.google.com/                            |     1 | USA       
  5 | FaceBook             | http://www.facebook.com/                           |     3 | USA       
(5 rows)


A=> select * from Websites order by country,alexa,id;
 id |         name         |                        url                         | alexa |  country   
----+----------------------+----------------------------------------------------+-------+------------
  2 | TaoBao               | https://www.taobao.com/                            |    13 | CN        
  4 | WeiBo                | http://www.weibo.com/                              |    20 | CN        
  3 | CaiNiao              | http://www.runoob.com/                             |  4689 | CN        
  1 | Google               | https://www.google.com/                            |     1 | USA       
  5 | FaceBook             | http://www.facebook.com/                           |     3 | USA       
(5 rows)


ORDER BY 多列的时候,先按照第一个column name排序,在按照第二个column name排序;如上述教程最后一个例子:

  • 1)、先将country值这一列排序,同为CN的排前面,同属USA的排后面;
  • 2)、然后在同属CN的这些多行数据中,再根据alexa值的大小排列。
  • 3)、ORDER BY 排列时,不写明ASC DESC的时候,默认是ASC。 

原创粉丝点击