常用SQL笔记

好记性不如烂笔头, 总结下常用SQL:
表名:emp
字段:name, age, grade,comm(奖金)

一、简单查询

  1. 查询全部
    select * from 表名;
    例如: select * from emp;

  2. 查询具体字段/列
    select 列名1,列名2 from 表名;
    例如:select name from emp;

  3. 删除重复值查询
    select DISTINCT 具体的列名 from 表名;
    例如:select distinct name from emp;

  4. 按指定格式返回:查询员工的姓名,年龄,显示格式为:姓名:张三,年龄:19
    select ‘姓名:’ || name || ', 年龄: ’ || age from emp;

    注意:Oracle 中提供的字符串连接操作,使用“||”表示。
    如果要加入一些显示信息的话,所有的其他的固定信息要使用’ '括起来。

二、限定查询(where)

  1. 限定查询语法
    select 具体的列 别名 from 表名 where 条件;
    例如:select * from emp where age >35; (年龄大于35岁的所有员工)

  2. 不为空的表示:字段 IS NOT NULL
    例如: 查询获得奖金的员工 select * from emp where comm IS NOT NULL;
    查询没有获取奖金的员工: select * from emp where comm IS NULL;

  3. 同时满足多个条件查询 AND,OR
    例如: 成绩在90分以上并且拿到奖金的员工 select * from emp where grade >90 AND comm IS NOT NULL;
    成绩在90分以上或者拿到奖金的员工 select * from emp where grade >90 OR comm IS NOT NULL;

  4. 使用 NOT 可以取反,把真的条件变为假的,假的变为真的。
    例如:查询成绩不在90分以上,同时没有得到奖金的员工, 相当于整体取反:
    select * from emp where NOT (grade >90 AND comm IS NOT NULL);

  5. 查询年龄在 30-40岁之间的员工: select * from emp where age>30 and age<40;
    使用BETWEEN…AND… : select * from emp WHERE age BETWEEN 30 AND 40;

  6. 日期
    1981年1月1日 ~ 1981年12月31日之间雇佣的雇员· 日期表示的时候要加“ ’ ”
    select * from emp WHERE hiredate BETWEEN ‘1-1 月-81’ AND ‘31-12 月 -81’ ;

  7. 指定查询范围
    在范围内: 字段 IN (值1,值2,…)
    不在范围内: 字段 NOT IN (值1,值2,…)
    例如: 查询成绩为90, 95, 100 的员工: select * from emp where grade IN (90,95,100);
    查询成绩不是90, 95, 100 的员工: select * from emp where grade NOT IN (90,95,100);

  8. 模糊查询 LIKE
    % 可以匹配任意长度内容 ; _ 可以匹配一个长度的内容
    例如:查询员工姓名中第二个字母包含"M"的员工信息 : select * from emp where name LIKE ‘_M%’ ;
    查询员工姓名中包含"M"的员工信息: select * from emp where name LIKE ‘%M%’ ;

  9. 对结果进行排序(ORDER BY)
    select * from 表名 where 条件 order by 字段1,字段2 ASC(DESC);
    ASC 表示升序、DESC 表示降序
    例如: 按照成绩由高到低: select * from emp ORDER BY grade DESC;

三、单行函数

语法:function_name(column|expression,[arg1,arg2,…])
· function_name:函数名称
· column:数据库列名
· expression:字符串或计算表达式
· arg1,arg2:在函数中使用参数

  1. 将小写字母变为大写字母 upper()
    select UPPER(‘smith’) from DUAL; —>输出:SMITH
    例如: select * from emp where name = UPPER(‘Smith’); —>输出:SMITH
    dual: Oracle提供的最小的工作表,只有一行一列,具有某些特殊功用

  2. 将一个字符串变为小写字母 lower()
    select LOWER(‘HELLO WORLD’) from dual; —>hello world

  3. 首字母大写 initcap()
    select initcap(‘HELLO WORLD’) from dual; —> Hello World
    例如: select initcap(name) from emp; —> 每个名字的首字母大写

  4. 字符串连接 concat()
    select concat(‘Hello’,‘world’) from dual; —>Hello world
    使用 || :select ‘hello’||‘word’ from dual; —>hello world

  5. 字符串截取:substr()
    第一个参数是源字符串,第二个参数是开始索引,第三个参数长度,开始的索引使用1和0效果相同
    select substr(‘hello’,1,3) from dual; —>hel
    实例:查询员工姓名及其员工姓名的后三位字符 : select name, substr(name,-3,3) from emp;

  6. 获取字符串的长度length()
    select length(‘hello’) from dual; —>5

  7. 字符串替换:
    第一个参数是源字符串,第二个参数被替换的字符串,第三个是替换字符串
    select replace(‘hello’, ‘l’,‘z’) from dual; —>hezzo

  8. 四舍五入round(x [,n])
    select round(66.66) from dual; ---->67
    保留2位小数:select round(66.666,2) from dual; —>66.67

    n可以是负数,表示在整数位部分四舍五入:
    select round(66.66,-1) from dual;–>70
    select round(666.66,-2) from dual; —>700

  9. 截断小数位:TRUNC() (不会四舍五入)
    select trunc(66.66) from dual; —>66
    select trunc(66.66,1) from dual; ---->66.6
    select trunc(666.66,-2) from dual ; —>600

  10. 取余MOD()
    select mod(10,3) from dual ; —>1

参考连接:
https://www.jianshu.com/p/67007daf2d94
https://blog.csdn.net/qq_36648496/article/details/88852655

欢迎补充反馈,三克油~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值