zoukankan      html  css  js  c++  java
  • 一些简单的查询语法

    计算列:取出表中某些记录的指定字段进行运算。

    select * from emp;
        --*表示所有的
        --from emp 表示从emp表查询
    
    select empno,ename from emp;
    
    select ename,sal from emp;
    
    select ename,sal*12 as "年薪" from emp;
        --as可以省略 记录 “年薪”不要写成‘年薪’ 也不要写成 年薪
    
    select ename,sal*12 as "年薪", sal "月薪",job from emp;
    
    select 888 from emp;
        --ok
        --输出的行数是emp的行数,每行只有一个字段,值是888
    
    select 5;    --ok    
                --不推荐

    distinct添加在第一个字段前,用于过滤重复的记录。

    select deptno from emp; --14行记录 不是3行记录
    
    select distinct deptno from emp; --distinct deptno 会过滤重复的deptno
    select distinct comm from emp; --distinct也可以过滤掉重复的null
    select distinct comm,deptno from emp; --把comm和deptno的组合进行过滤
    select deptno,distinct comm from emp; --error 逻辑上有冲突
    
    select 10000 from emp; --14行记录

    between:划定某个范围内的数据。

    --查找工资在1500到3000之间(包括1500和3000)的所有的员工的信息
    select * from emp
        where sal >= 1500 and sal <= 3000
    
    --等价于
    
    select * from emp
        where sal between 1500 and 3000
    
    --查找工资在小于1500或者大于3000之间(包括1500和3000)的所有的员工的信息
    select * from emp
        where sal < 1500 or sal > 3000
    
    
    --等价于
    
    select * from emp
        where sal not between 1500 and 3000

    in:指定列的值是不是属于一个或多个孤立的值?

    select * from emp where sal in (1500,3000,5000)
    --等价于
    
    select * from emp
        where sal = 1500 or sal = 3000 or sal = 5000
    
    select * from emp where sal not in (1500,3000,5000) --把sal既不是1500,也不是3000,也不是5000的记录输出
    --等价于
    
    select * from emp
        where sal <> 1500 and sal <> 3000 and sal <> 5000
            --数据库中不等于有两种表示: !=  <> 推荐使用第二种
            --对或取反是并且 对并且取反是或

    top:前面的若干个记录。

    select * from emp;
    
    select top 5 * from emp;  --取出记录前5条
    
    select top 15 percent * from emp; --输出的是三个,不是两个(向上取整)

    null:

    isnull函数:isnull(字段A,数值B),如果指定的字段A为null,则返回数值B。否则返回字段A的数值。

    select * from emp
    --输出奖金飞空的员工的信息
    select * from emp where comm <> null; --输出为空error
    select * from emp where comm != null; --输出为空error
    select * from emp where comm = null;  --输出为空error
        --null不能参与<> != =运算
    
    --null可以参与is not, is
    select * from emp where comm is null; --输出奖金为空的员工的信息
    select * from emp where comm is not null; --输出奖金不为空的员工的信息
    
    
    --任何类型的数据都允许为null
    create table t1( name nvarchar(20),cnt int,riqi datetime );
    insert into t1 values( null,null,null );
    select * from t1;
        
    --输出每个员工的姓名 年薪(包含了奖金)
    select ename,sal*12 + comm as "年薪" from emp;
    --本程序证明了:null不能参与任何数据运算,否则结果永远为空
    
    --输出每个员工的姓名 年薪(包含了奖金)
    select ename,sal*12 + isnull(comm,0) as "年薪" from emp;
    --isnull(comm,0)如果comm是null,就返回零,否则返回具体的值.

    order by:以某个字段排序。

    如果不指定排序标准,则默认是升序,升序用asc表示,默认可以不写。

    为一个字段指定的排序标准并不会对另一个字段产生影响。(强烈建议为每一个字段都制定排序的标准

    --asc是升序的意思 默认可以不写 desc是降序
    select * from emp order by sal; --默认是按照升序排序
    select * from emp order by deptno,sal;--先按照deptno升序排序,如果deptno相同,再按照sal升序排序
    
    select * from emp order by deptno desc,sal;
        --先按照deptno降序排序,如果deptno相同,再按照sal升序排序
        --记住sal是升序不是降序
        --order by a desc,b,c,d des只对a产生影响,不会对后面b.c.d产生影响
    
    select * from emp order by deptno,sal desc
        --问题:desc是否会对deptno产生影响?
        --答案:不会
        --先按照deptno升序,如果deptno相同,再按sal降序

    注:本文参考了郝斌老师的SQL教程,也加入了自己对SQL的一些理解,有写的不对的地方希望大家能够指出来。

  • 相关阅读:
    分享一些优化博客的心得
    HTTP相关整理(上)
    正则&highlight高亮实现(干货)
    通过一个demo了解Redux
    升级ChinaCock10.3.3激情版3注意事项
    解决uniDAC超时问题:已超过了锁请求超时时段。
    kbmMWSmartBind实现ListView绑定数据集进阶篇(一)
    通过输出日志到文件来检查app闪退原因
    VertScrollBox不能滑动
    使用kbmMW SmartBinding经验几则
  • 原文地址:https://www.cnblogs.com/ZRBYYXDM/p/5253577.html
Copyright © 2011-2022 走看看