好记性不如烂笔头, 总结下常用SQL:
表名:emp
字段:name, age, grade,comm(奖金)
一、简单查询
-
查询全部
select * from 表名;
例如: select * from emp; -
查询具体字段/列
select 列名1,列名2 from 表名;
例如:select name from emp; -
删除重复值查询
select DISTINCT 具体的列名 from 表名;
例如:select distinct name from emp; -
按指定格式返回:查询员工的姓名,年龄,显示格式为:姓名:张三,年龄:19
select ‘姓名:’ || name || ', 年龄: ’ || age from emp;注意:Oracle 中提供的字符串连接操作,使用“||”表示。
如果要加入一些显示信息的话,所有的其他的固定信息要使用’ '括起来。
二、限定查询(where)
-
限定查询语法
select 具体的列 别名 from 表名 where 条件;
例如:select * from emp where age >35; (年龄大于35岁的所有员工) -
不为空的表示:字段 IS NOT NULL
例如: 查询获得奖金的员工 select * from emp where comm IS NOT NULL;
查询没有获取奖金的员工: select * from emp where comm IS NULL; -
同时满足多个条件查询 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; -
使用 NOT 可以取反,把真的条件变为假的,假的变为真的。
例如:查询成绩不在90分以上,同时没有得到奖金的员工, 相当于整体取反:
select * from emp where NOT (grade >90 AND comm IS NOT NULL); -
查询年龄在 30-40岁之间的员工: select * from emp where age>30 and age<40;
使用BETWEEN…AND… : select * from emp WHERE age BETWEEN 30 AND 40; -
日期
1981年1月1日 ~ 1981年12月31日之间雇佣的雇员· 日期表示的时候要加“ ’ ”
select * from emp WHERE hiredate BETWEEN ‘1-1 月-81’ AND ‘31-12 月 -81’ ; -
指定查询范围
在范围内: 字段 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); -
模糊查询 LIKE
% 可以匹配任意长度内容 ; _ 可以匹配一个长度的内容
例如:查询员工姓名中第二个字母包含"M"的员工信息 : select * from emp where name LIKE ‘_M%’ ;
查询员工姓名中包含"M"的员工信息: select * from emp where name LIKE ‘%M%’ ; -
对结果进行排序(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:在函数中使用参数
-
将小写字母变为大写字母 upper()
select UPPER(‘smith’) from DUAL; —>输出:SMITH
例如: select * from emp where name = UPPER(‘Smith’); —>输出:SMITH
dual: Oracle提供的最小的工作表,只有一行一列,具有某些特殊功用 -
将一个字符串变为小写字母 lower()
select LOWER(‘HELLO WORLD’) from dual; —>hello world -
首字母大写 initcap()
select initcap(‘HELLO WORLD’) from dual; —> Hello World
例如: select initcap(name) from emp; —> 每个名字的首字母大写 -
字符串连接 concat()
select concat(‘Hello’,‘world’) from dual; —>Hello world
使用 || :select ‘hello’||‘word’ from dual; —>hello world -
字符串截取:substr()
第一个参数是源字符串,第二个参数是开始索引,第三个参数长度,开始的索引使用1和0效果相同
select substr(‘hello’,1,3) from dual; —>hel
实例:查询员工姓名及其员工姓名的后三位字符 : select name, substr(name,-3,3) from emp; -
获取字符串的长度length()
select length(‘hello’) from dual; —>5 -
字符串替换:
第一个参数是源字符串,第二个参数被替换的字符串,第三个是替换字符串
select replace(‘hello’, ‘l’,‘z’) from dual; —>hezzo -
四舍五入round(x [,n])
select round(66.66) from dual; ---->67
保留2位小数:select round(66.666,2) from dual; —>66.67n可以是负数,表示在整数位部分四舍五入:
select round(66.66,-1) from dual;–>70
select round(666.66,-2) from dual; —>700 -
截断小数位: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 -
取余MOD()
select mod(10,3) from dual ; —>1
参考连接:
https://www.jianshu.com/p/67007daf2d94
https://blog.csdn.net/qq_36648496/article/details/88852655
欢迎补充反馈,三克油~