zoukankan      html  css  js  c++  java
  • SQL函数升序Asc,降序Desc使用总结

    关键字-升序Asc及降序Desc的使用语法

    对某一结果集按列进行升序或降序排列即:结果集 Order by 列名/数字 Asc/Desc。

    一、Asc,Desc排序讲以下5点

    1、不写关键字Asc/Desc,默认按Asc排序

    2、列名的多种代替方式

    3、NULL是列中的最大值

    4、多个列排序

    二、数据准备

    --建表
    create table test_A  (  id SMALLINT not null primary key,  name varchar(10),age  SMALLINT );
    --插入数据
    insert into test_A values(0,'ZhangSan',23);
    insert into test_A values(1,'LiSi',21); 
    insert into test_A values(2,'WangWu',23); 
    insert into test_A values(3,'MaLiu',null);
    insert into test_A values(4,'maLiu',24);

    三、详细展示

    1、不写关键字Asc/Desc,默认按Asc排序

    --以下写法效果一样
    select * from test_A order by ID 
    select * from test_A order by ID Asc

    2、列名的多种代替方式

    --按ID升序排列的多种写法
    select * from test_A order by ID Asc
    --列名可用编号1,2,3...代替
    select * from test_A order by 1 Asc
    /*
    对于列的编号可以同COLNO+1的值获得
    select name,COLNO+1 from sysibm.syscolumns where tbname='TEST_A'
    */
    --列名可以用别名
    select id A_ID,name,age from test_A order by A_ID Asc

    3、NULL是列中的最大值

    --Age列存在空值,按Age升序排列
    select * from test_A order by Age Asc

    --Age存在空值,按Age降序排列
    select * from test_A order by Age desc


    4、多个列排序,关键字Asc,Desc只对左侧紧挨着的这一列起作用

    --按ID降序,Age升序
    select * from test_A order by ID,Age desc

  • 相关阅读:
    HDU 4864 Task(贪心值得学习)
    使程序在Linux下后台运行
    KMP算法
    优先队列的使用
    POJ 2761 Feed the dogs(树状数组求区间第K大)
    HDU 3584 Cube (三维树状数组)
    HDU 1892 See you~ (二维树状数组)
    POJ 1195 Mobile phones(二维树状数组)
    HDU 1166 敌兵布阵 (树状数组和线段树解法)
    POj 1703 Find them, Catch them(关系并查集)
  • 原文地址:https://www.cnblogs.com/handhead/p/11023655.html
Copyright © 2011-2022 走看看