数据库的常见查询操作

简介: 数据库都通用这是orcl的–员工表信息 select *from emp; –部门表的所有信息 select *from dept; –查询员工表的所有编号,名字,工资 sele...

数据库都通用这是orcl的

–员工表信息
select *from emp;
–部门表的所有信息
select *from dept;
–查询员工表的所有编号,名字,工资
select empno,ename,sal from emp;
–查询员工表的编号,名字,工资,年薪
select empno,ename,sal,sal*12 from emp;
–as别名小名
select empno as 员工编号,ename as 员工姓名,sal as 员工工资 from emp;
–双引号的作用
select empno “en”from emp;
–连接符||
select ename ||’的月薪是’||sal from emp;
–去除重复的行
select distinct ename,sal from emp;
–order by 进行排序asc 升序desc降序
–查询所有数据对工资进行升序排序
select *from emp order by sal asc;
–降序
select *from emp order by sal desc;
–多字段排序分主次
select *from emp order by sal asc,empno desc;
select ename as 姓名,empno as 编号 ,sal as 工资 from emp sal order by 编号 desc, 工资 asc;
–where条件查询
–查询名叫SCOTT的员工信息
select *from emp where ename =’SCOTT’;
–查询工资为1250
select *from emp where sal=1250;
–查询入职时间1981-2-22的信息
select *from emp where hiredate=’22-2月-81’;
–大于
select *from emp where hiredate >’1-1月-81’;
–小于
select *from emp where hiredate <’1-1月-81’;
–查询工资在1250及以上的员工
select *from emp where sal>=1250;
–查询工资不等于800的员工
select *from emp where sal!=800;
select *from emp where sal<>800;
–查询工资在800-1000;
select *from emp where sal between 800 and 1000;
select *from emp where sal between 800 and 1000;
–in集合
–查询800-900-1000-1250的员工信息
select *from emp where sal in(800,1000,900,1250);
–查询奖金为空的员工信息
select *from emp where comm is null;
select *from emp where comm=”;
–查询奖金不为空的
select *from emp where comm is not null;
select *from emp where comm !=”;
–like模糊查询通配符
–查询以s开头的
select *from emp where ename like ‘S%’;
select *from emp where ename like ‘D__%’;
–用escape来强调是,
select *from emp where ename like ‘%\%’escape’\’;
–and or not 运算符
–查询工资大于900并且以s开头
select *from emp where sal>900 and ename like ‘S%’;
–或者以s
select *from emp where sal>900 or ename like ‘S%’;
–常用字符函数
–查询员工表的姓名并且首字母大写
select initcap(ename)from emp;
–小写
select lower(ename)from emp;
–全大写
select upper(ename)from emp;
–左移除
select ltrim(‘scdn’,’sc’)from dual;
–右边移除
select rtrim(‘csdn’,’dn’)from dual;
–左右移除空格
select ltrim(rtrim(’ bfdhugf ‘))from dual;
–翻译
select translate(‘dfsff’,’df’,’lj’)from dual;
–替换
select replace(‘dfsfd’,’df’,’dss’)from dual;
–查找出现的位置
select instr(‘fdsafs’,’s’)from dual;
–截取
select substr(‘sgfyusg’,3,2)from dual;
–连接
select concat(‘dfdsfd’,’fdsfd’)from dual;
select ‘dfdf’||’dfdfa’from dual;
–绝对值
select abs(-15)from dual;
–x的y次幂
select power(2,3)from dual;
–向上取整
select ceil(12.5)from dual;
–向下取整
select floor(12.5)from dual;
–截断保留
select trunc(12.456,2)from dual;
–四舍五入
select round(122.456,2)from dual;
–开平方
select sqrt(4)from dual;
–取余数
select mod(10,3)from dual;
–取符号
select sign(-25)from dual;
select sign(12)from dual;
–两个日期间隔月份
select months_between(sysdate,’8-12月-1998’)from dual;
–修改月份
select add_months(sysdate,1)from dual;
select add_months(sysdate,-1)from dual;
–返回指定的一个星期几的日期
select next_day(‘16-8月-17’,’星期日’)from dual;
–日期的四舍五入
select round (sysdate,’year’)from dual;
select round (sysdate,’month’)from dual;
select round (sysdate ,’day’)from dual;
–日期转字符串
select to_char(sysdate,’yyyy-mm-dd’)from dual;
–字符串转日期
select to_date(‘2017年8月16日’,’yyyy”年”mm”月”dd”日”’)from dual;
–字符串转数值型
select to_number(‘123,549,654,12.789  ,   999,999,999,99.999’)from dual;
select to_number(‘¥123,456,46.4567’,’L999,999,99.9999’)from dual;
–数值转字符串
select to_char(12345678.123,’L999,999,99.999’)from dual;
–时间比较问题
select *from emp where hiredate>’1-1月-81’;
select *from emp where hiredate>to_date(‘1981-1-1’,’yyyy-mm-dd’);
select *from emp where to_char(hiredate,’yyyy-mm-dd’)>’1998-1-1’;
–空转数
select ename,nvl(comm,0)from emp;
–nvl2(e,n,m)e为空m不为空转n
select ename,nvl2(comm,comm,0)from emp;
select decode(sal,1250,’有钱’,5000,’真有钱’,’穷鬼’)from emp;
–case when else end (if else)
select (case
when sal >=5000 then ‘有钱’
when sal>=4000 then ‘1有钱’
when sal>=3000 then ‘2有钱’
else ‘穷’
end)from emp;
select (case sex when 0 then’男’when 1 ‘女’ end)from dual;
–sum求和 max最大值min最小值avg平均值count统计
select max(sal)from emp;
select min(sal)from emp;
select avg(sal)from emp;
select sum(sal)from emp;
select count(sal)from emp;
–多少员工
select count(*)from emp;
–聚合函数可以写在一行上
select max(sal),min(sal),sum(sal),avg(sal),count(*)from emp;
–统计所有(*)
select count(*)from emp;
select count (distinct sal)from emp;
–查寻部门编号为10的员工信息
select *from emp;
select *from emp where deptno like ‘10%’;
–查寻年薪大于三万姓名雨部门编号
select ename,empno,sal*12 from emp where sal*12>30000;
–查询佣金为null姓名工资
select ename as 姓名,sal as 工资 from emp where comm is null;
–查询工资大于1500and有佣金的人
select ename as 姓名, sal as 工资 from emp where sal>1500 and comm is not null;
–查询工资大于1500or有佣金的人
select ename as 姓名, sal as 工资 from emp where sal>1500 or comm is null;
–查询姓名里有S的员工的信息工资名字
select ename as 姓名, sal as 工资 from emp where ename like ‘S%’;
–查询以j开头第二个字符是o 的
select ename as 姓名, sal as 工资 from emp where ename like ‘jo%_’;
select*from emp where ename like ‘% ’;

相关文章
|
2天前
|
数据库
|
20天前
|
存储 人工智能 监控
时序数据库 TDengine 化工新签约:存储降本一半,查询提速十倍
化工行业在数字化转型过程中面临数据接入复杂、实时性要求高、系统集成难度大等诸多挑战。福州力川数码科技有限公司科技依托深厚的行业积累,精准聚焦行业痛点,并携手 TDengine 提供高效解决方案。
33 0
|
2月前
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
168 6
|
2月前
|
缓存 关系型数据库 MySQL
【深入了解MySQL】优化查询性能与数据库设计的深度总结
本文详细介绍了MySQL查询优化和数据库设计技巧,涵盖基础优化、高级技巧及性能监控。
521 0
|
3月前
|
存储 缓存 网络协议
数据库执行查询请求的过程?
客户端发起TCP连接请求,服务端通过连接器验证主机信息、用户名及密码,验证通过后创建专用进程处理交互。服务端进程缓存以减少创建和销毁线程的开销。后续步骤包括缓存查询(8.0版后移除)、语法解析、查询优化及存储引擎调用,最终返回查询结果。
58 6
|
4月前
|
SQL 安全 Java
MyBatis-Plus条件构造器:构建安全、高效的数据库查询
MyBatis-Plus 提供了一套强大的条件构造器(Wrapper),用于构建复杂的数据库查询条件。Wrapper 类允许开发者以链式调用的方式构造查询条件,无需编写繁琐的 SQL 语句,从而提高开发效率并减少 SQL 注入的风险。
75 1
MyBatis-Plus条件构造器:构建安全、高效的数据库查询
|
3月前
|
SQL JavaScript 程序员
数据库LIKE查询屡试不爽?揭秘大多数人都忽视的秘密操作符!
本文分析了因数据库中的不可见空白字符导致的数据查询问题,探讨了问题的成因与特性,并提出了使用 SQL 语句修复问题的有效方案。同时,总结了避免类似问题的经验和注意事项。
52 0
|
4月前
|
存储 缓存 关系型数据库
怎么让数据库查询更快
【10月更文挑战第25天】通过以上综合的方法,可以有效地提高数据库查询的速度,提升应用程序的性能和响应速度。但在优化过程中,需要根据具体的数据库系统、应用场景和数据特点进行合理的调整和测试,以找到最适合的优化方案。
|
4月前
|
存储 缓存 固态存储
怎么让数据库查询更快
【10月更文挑战第28天】
65 2
|
4月前
|
监控 关系型数据库 MySQL
数据库优化:MySQL索引策略与查询性能调优实战
【10月更文挑战第27天】本文深入探讨了MySQL的索引策略和查询性能调优技巧。通过介绍B-Tree索引、哈希索引和全文索引等不同类型,以及如何创建和维护索引,结合实战案例分析查询执行计划,帮助读者掌握提升查询性能的方法。定期优化索引和调整查询语句是提高数据库性能的关键。
763 1

热门文章

最新文章