zoukankan      html  css  js  c++  java
  • SQL COOKBOOK SQL经典实例代码 笔记第一章代码

    -- SQL COOKBOOK CHAPTER1
    -- 查看所有内容
    select * from emp;
    
    -- 可以单列
    select empno,ename,job,sal,mgr,hiredate,comm,deptno
    from emp;
    
    -- 设定条件
    select * from emp
    where deptno=10;
    
    -- 条件可以用逻辑运算符连接
    select * from emp
    where deptno=10
    or comm is null
    or sal<=2000
    and deptno=20;
    
    -- 注意括号和顺序
    select * from emp
    where (
        deptno=10 
        or comm is not null 
        or (deptno=20 and sal<=2000)
    );
    
    -- 起有意义的别名
    select sal as salary, comm  as commission from emp;
    
    -- 使用子查询可以在条件中用别名筛选
    select * from 
    (
        select sal as salary, comm as commission
        from emp
    )x
    where salary<5000;
    
    -- 代码执行的顺序:from -> where -> select 
    
    select ename, job
    from emp
    where deptno=10;
    
    -- 连接符号
    select ename||' WORKS AS A '||job as msg 
    from emp where deptno=10;
    -- || 是concat的快捷方式
    select concat(ename,' WORKS AS A ', job) FROM EMP where deptno=10;
    
    -- case when
    select ename,sal,
    case when sal<=2000 then 'UNDERPAID'
        when sal>=4000 then 'OVERPAID'
        ELSE 'OK'
    end as status
    from emp;
    
    -- 取前5行
    select * from emp limit 5;
    -- 任意序列的前5行
    select ename, job from emp order by random() limit 5;
    
    -- 查询空值
    select * from emp where comm is null;
    -- 空值置换
    select coalesce(comm, 0) from emp;
    -- in的使用
    select ename,job,deptno from emp where deptno in (10, 20);
    -- like的使用
    select ename,job from emp where deptno in (10,20) and (ename like '%I%' or job like '%ER')
    
  • 相关阅读:
    Mysql 视图用途、使用场景、性能问题及使用注意事项
    深入解析MySQL视图VIEW
    delete语句的基本用法
    update语句基本用法
    mysql插入中文数据变成问号怎么处理
    MySQL数据表中有自增长主键时如何插入数据
    INSERT INTO语句的基本用法
    mysql HAVING用法
    mysql关于group by的用法
    python模拟鼠标拖动教程
  • 原文地址:https://www.cnblogs.com/heenhui2016/p/11623711.html
Copyright © 2011-2022 走看看