本人寫的所有的文檔都是使用的MySQL版本為5.7.2
order by主要用來排序檢索數(shù)據(jù)。可以與desc連用。也可以與limit進(jìn)行連用。
1.排序檢索單列:
這是一條檢索單列排序的語句:
select columns from table_name order by columns;
# 示例如下:
select prod_price from products order by prod_price;

檢索單列數(shù)據(jù)
2.檢索多列數(shù)據(jù)
在這里檢索多列數(shù)據(jù)的時候,首先按照prod_name排序,然后按照prod_price進(jìn)行排序。(默認(rèn)是從小到大。數(shù)字在前,然后是字母)
select columns1, columns2, columns3 from table_name from columns2, columns3;
示例如下:
select prod_id, prod_name, prod_price from products order by prod_name, prod_price;

檢索多列數(shù)據(jù)
3.多列數(shù)據(jù)倒序檢索
在此需要注意的是,此處只按照了第一列倒序進(jìn)行排列。如果每個字段都需要倒序的話,那么你就需要在每個字段后面都要寫上desc。默認(rèn)是正序。(ascending--> ASCENDING)
select columns1, columns2, columns3 from tables order by columns1 desc, columns2;
# 示例如下
select prod_id, prod_name, prod_price from products order by prod_price desc ,prod_name;

多列數(shù)據(jù)檢索