• sql基本语句(一)select


    1.使用sys对scott用户进行解锁和重置密码:

    alter user scott identified by tiger account unlock;

    2.书写sql语句

      1)sql语句不区分大小写

      2)sql语句可以分成一行或者多行写

      3)关键字不可以简写或者多行分写

      4)子句通常分行写

      5)使用缩进提高可读性

      6)关键字通常用大写,其他的词像表名或者列名用小写

    3.可以将显示的结果直接进行算术运行,产生的结果不改变数据库中的值,称为伪列

    SQL> select ename,sal*1.1 from emp;  //将工资乘以1.1倍显示
    
    ENAME                   SAL*1.1
    -------------------- ----------
    SMITH                       880
    ALLEN                      1760
    WARD                       1375
    JONES                    3272.5
    MARTIN                     1375
    BLAKE                      3135
    CLARK                      2695
    SCOTT                      3300
    KING                       5500
    TURNER                     1650
    ADAMS                      1210
    
    ENAME                   SAL*1.1
    -------------------- ----------
    JAMES                      1045
    FORD                       3300
    MILLER                     1430
    
    14 rows selected.

     4.连接操作符

      1)将多列连接在一起显示

    USER is "SCOTT"
    SQL> select ename||job from emp;  //将ename列和job列连接在一起显示
    
    ENAME||JOB
    --------------------------------------
    SMITHCLERK
    ALLENSALESMAN
    WARDSALESMAN
    JONESMANAGER
    MARTINSALESMAN
    BLAKEMANAGER
    CLARKMANAGER
    SCOTTANALYST
    KINGPRESIDENT
    TURNERSALESMAN
    ADAMSCLERK
    
    ENAME||JOB
    --------------------------------------
    JAMESCLERK
    FORDANALYST
    MILLERCLERK
    
    14 rows selected

       2)将多列连接在一起显示,中间可以插入其它字符

    SQL> select ename|| ' is a ' || job from emp;  //两列中间插入 is a
    
    ENAME||'ISA'||JOB
    --------------------------------------------------
    SMITH is a CLERK
    ALLEN is a SALESMAN
    WARD is a SALESMAN
    JONES is a MANAGER
    MARTIN is a SALESMAN
    BLAKE is a MANAGER
    CLARK is a MANAGER
    SCOTT is a ANALYST
    KING is a PRESIDENT
    TURNER is a SALESMAN
    ADAMS is a CLERK
    
    ENAME||'ISA'||JOB
    --------------------------------------------------
    JAMES is a CLERK
    FORD is a ANALYST
    MILLER is a CLERK
    
    14 rows selected.

       3)善用连接符。如生成删除表的命令,可以放到脚本中运行。

    SQL> select 'drop table ' ||tname||';'from tab;  //删除表
    
    'DROPTABLE'||TNAME||';'
    --------------------------------------------------------------------------------
    drop table BONUS;
    drop table DEPT;
    drop table EMP;
    drop table SALGRADE;

     4.压缩重复的行。只有查询的列中每一行都重复的情况下,才会压缩显示。

    SQL>  select distinct deptno from emp;
    
        DEPTNO
    ----------
            30
            20
            10
    
    SQL>  select distinct deptno,job from emp;
    
        DEPTNO JOB
    ---------- ------------------
            20 CLERK
            30 SALESMAN
            20 MANAGER
            30 CLERK
            10 PRESIDENT
            30 MANAGER
            10 CLERK
            10 MANAGER
            20 ANALYST
    
    9 rows selected.

     5.限制返回的行信息

      1)where 条件: 对条件进行判断,如果值为true 则执行,否则不执行。这里的条件可以是逻辑语句。与 and、或 or、非 not,这几个逻辑词可一起使用。

    SQL> select job from emp where not sal<1000;  //工资大于等于1000的工作。
    
    JOB
    ------------------
    SALESMAN
    SALESMAN
    MANAGER
    SALESMAN
    MANAGER
    MANAGER
    ANALYST
    PRESIDENT
    SALESMAN
    CLERK
    ANALYST
    CLERK
    
    12 rows selected.

      2)where 条件:条件是字符串的时候,大小写敏感。条件是日期的时候,格式敏感,且这两者都要放到单引号中。

    SQL> select * from emp where hiredate='17-nov-81';  //查询日期的时候,大小写不区分,但是格式敏感。
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7839 KING                 PRESIDENT                     17-NOV-81          5000                    10

      3)where 条件:between 1000 and 3000,闭区间,大于等于1000小于等于3000。

    SQL> select job from emp where sal between 1000 and 3000;  //选出工资在1000到3000之间的工作
    
    JOB
    ------------------
    SALESMAN
    SALESMAN
    MANAGER
    SALESMAN
    MANAGER
    MANAGER
    ANALYST
    SALESMAN
    CLERK
    ANALYST
    CLERK
    
    11 rows selected.

      4)where 条件:in {' ',' '},是一个枚举型,括号里面跟随的是集合列表,可以跟随多个元素,但是类型必须一致。

    SQL> select * from emp where job in ('CLERK','SALESMAN');  //查看clerk和salesman职员的详细信息
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7369 SMITH                CLERK                    7902 17-DEC-80           800                    20
          7499 ALLEN                SALESMAN                 7698 20-FEB-81          1600        300         30
          7521 WARD                 SALESMAN                 7698 22-FEB-81          1250        500         30
          7654 MARTIN               SALESMAN                 7698 28-SEP-81          1250       1400         30
          7844 TURNER               SALESMAN                 7698 08-SEP-81          1500          0         30
          7876 ADAMS                CLERK                    7788 23-MAY-87          1100                    20
          7900 JAMES                CLERK                    7698 03-DEC-81           950                    30
          7934 MILLER               CLERK                    7782 23-JAN-82          1300                    10
    
    8 rows selected.

      5)where 条件:is null,对值是否为空值进行判断

    SQL> select * from emp where comm is null;  //查看奖金为空值的信息
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7369 SMITH                CLERK                    7902 17-DEC-80           800                    20
          7566 JONES                MANAGER                  7839 02-APR-81          2975                    20
          7698 BLAKE                MANAGER                  7839 01-MAY-81          2850                    30
          7782 CLARK                MANAGER                  7839 09-JUN-81          2450                    10
          7788 SCOTT                ANALYST                  7566 19-APR-87          3000                    20
          7839 KING                 PRESIDENT                     17-NOV-81          5000                    10
          7876 ADAMS                CLERK                    7788 23-MAY-87          1100                    20
          7900 JAMES                CLERK                    7698 03-DEC-81           950                    30
          7902 FORD                 ANALYST                  7566 03-DEC-81          3000                    20
          7934 MILLER               CLERK                    7782 23-JAN-82          1300                    10
    
    10 rows selected.
    
    
    SQL> select * from emp where comm is not null;  //查看奖金为非空值的信息
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7499 ALLEN                SALESMAN                 7698 20-FEB-81          1600        300         30
          7521 WARD                 SALESMAN                 7698 22-FEB-81          1250        500         30
          7654 MARTIN               SALESMAN                 7698 28-SEP-81          1250       1400         30
          7844 TURNER               SALESMAN                 7698 08-SEP-81          1500          0         30

      6)where 条件:like 模糊匹配

    SQL> select * from emp where ename like 'A%';  //%匹配所有的意思
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7499 ALLEN                SALESMAN                 7698 20-FEB-81          1600        300         30
          7876 ADAMS                CLERK                    7788 23-MAY-87          1100                    20
    
    SQL> select * from emp where ename like '_O%';  //'_'单个通配符
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7566 JONES                MANAGER                  7839 02-APR-81          2975                    20
          7902 FORD                 ANALYST                  7566 03-DEC-81          3000                    20
    
    
    SQL> select * from emp where ename like '_/_%'escape'/';  //escape,可以将任意字符定义转义字符,这里是将/定义为转义字符。
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7788 X_SCOTT              ANALYST                  7566 19-APR-87          3000                    20

       7)结果集中有一个oracle隐藏的伪列 ,称为结果集的编号列rownum列。

    scott@TEST>select ename,sal from(select * from emp order by sal desc) where rownum < 4;  //对工资进行排序
    
    ENAME                       SAL
    -------------------- ----------
    KING                       5000
    SCOTT                      3000
    FORD                       3000
    scott@TEST>select ename,sal from (select rownum bb,ename,sal from (select rownum ,ename,sal from emp order by sal desc))where bb=2; //找出第二名工资
    
    ENAME                       SAL
    -------------------- ----------
    FORD                       3000

    6.order by 子句来进行排序操作

      默认是升序 asc

      降序时指定 desc

      这里也可以进行多列排序,按照先后顺序进行排序。

    SQL> select * from emp order by sal;  //对工资进行升序排序
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7369 SMITH                CLERK                    7902 17-DEC-80           800                    20
          7900 JAMES                CLERK                    7698 03-DEC-81           950                    30
          7876 ADAMS                CLERK                    7788 23-MAY-87          1100                    20
          7521 WARD                 SALESMAN                 7698 22-FEB-81          1250        500         30
          7654 MARTIN               SALESMAN                 7698 28-SEP-81          1250       1400         30
          7934 MILLER               CLERK                    7782 23-JAN-82          1300                    10
          7844 TURNER               SALESMAN                 7698 08-SEP-81          1500          0         30
          7499 ALLEN                SALESMAN                 7698 20-FEB-81          1600        300         30
          7782 CLARK                MANAGER                  7839 09-JUN-81          2450                    10
          7698 BLAKE                MANAGER                  7839 01-MAY-81          2850                    30
          7566 JONES                MANAGER                  7839 02-APR-81          2975                    20
          7788 X_SCOTT              ANALYST                  7566 19-APR-87          3000                    20
          7902 FORD                 ANALYST                  7566 03-DEC-81          3000                    20
          7839 KING                 PRESIDENT                     17-NOV-81          5000                    10
    
    14 rows selected.
    
    SQL> select * from emp order by sal desc;  //对工资进行降序排序
    
         EMPNO ENAME                JOB                       MGR HIREDATE            SAL       COMM     DEPTNO
    ---------- -------------------- ------------------ ---------- ------------ ---------- ---------- ----------
          7839 KING                 PRESIDENT                     17-NOV-81          5000                    10
          7902 FORD                 ANALYST                  7566 03-DEC-81          3000                    20
          7788 X_SCOTT              ANALYST                  7566 19-APR-87          3000                    20
          7566 JONES                MANAGER                  7839 02-APR-81          2975                    20
          7698 BLAKE                MANAGER                  7839 01-MAY-81          2850                    30
          7782 CLARK                MANAGER                  7839 09-JUN-81          2450                    10
          7499 ALLEN                SALESMAN                 7698 20-FEB-81          1600        300         30
          7844 TURNER               SALESMAN                 7698 08-SEP-81          1500          0         30
          7934 MILLER               CLERK                    7782 23-JAN-82          1300                    10
          7521 WARD                 SALESMAN                 7698 22-FEB-81          1250        500         30
          7654 MARTIN               SALESMAN                 7698 28-SEP-81          1250       1400         30
          7876 ADAMS                CLERK                    7788 23-MAY-87          1100                    20
          7900 JAMES                CLERK                    7698 03-DEC-81           950                    30
          7369 SMITH                CLERK                    7902 17-DEC-80           800                    20
    
    14 rows selected.
    SQL> select sal,comm*2 from emp order by comm*2,sal;  //先按照奖金的顺序排序,然后在按照工资的顺序进行排序
    
           SAL     COMM*2
    ---------- ----------
          1500          0
          1600        600
          1250       1000
          1250       2800
           800
           950
          1100
          1300
          2450
          2850
          2975
          3000
          3000
          5000
    
    14 rows selected.
  • 相关阅读:
    介绍一篇关于session的好文章,写的很详细
    介绍一篇关于session的好文章,写的很详细
    Web文件的ContentType类型大全
    介绍一篇关于session的好文章,写的很详细
    C++面向对象学习1
    归并排序,不错~~
    在博客园写给自己
    简单的数字图形
    再不写博客就老了
    python日志按时间切分TimedRotatingFileHandler
  • 原文地址:https://www.cnblogs.com/sangmu/p/6753196.html
Copyright © 2020-2023  润新知