ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

数据库增删改查-1

2022-07-05 21:04:01  阅读:167  来源: 互联网

标签:name sal 数据库 改查 查询 emp 增删 where select


基础查询语句:select * from 表名           例如:select * from emp;  查询全部字段

查询一个字段:select 字段 from 表名     例如:select name from emp;  查询员工姓名

查询多个字段:select 字段,字段 from 表名   例如:select name,empno from emp;  查询员工姓名和员工编号

查询计算字段:例如编号,姓名,年薪:select empno,name,sal,sal*12 年薪 from emp;

查询(=,!=,<>)查询薪水不为5000的员工 :select * from emp where sal <> 5000;

范围查询:查询薪水为1600到3000的员工:select * from emp where sal>=1600 and sal<=3000;或(select * from emp where sal between 1600 and 3000;)

                  查询薪水不为1600到3000的员工:select * from emp where not (sal>=1600 and sal<=3000);或(select * from emp where sal not between 1600 and 3000;)

 

空值查询:查询津贴为空的员工:select * from emp where comm is null;(注意此处用isnull而不是=)     查询津贴不为空的员工:select * from emp where comm is not null;

 

多条件查询:工作岗位为MANAGER,薪水大于2500的员工:select * from emp where job= 'manger' and sal>2500;  薪水大于1800,并且部门代码为20或30的员工:select * from emp where sal>1800 and (deptno=20 or deptno=30);

包含查询(or,in):查询job 不为manager或者不为salesman的员工:select * from emp where not (job='manager' or job='salesman');  或者 select * from emp where job not in('manager' or 'salesman');

取反查询(not):查询薪水不为1600和不为3000的员工:第一种:select * from emp where sal !=1600 or sal !=3000; 第二种:select * from emp where not (sal = 1600 or sal = 3000);第三种:select * from emp where sal not in(1600,3000);

 

模糊查询(like):查询所有姓【张】的员工:select * from emp where name like '张%';

模糊查询(like *):查询姓名中包含【三】的所有的员工:select * from emp where name like '%三%';

模糊查询(like _):查询姓名中第二个字符为【三】的所有员工:select * from emp where namelike '_三%';

 

正则查询(regexp):查询name中包含A或B字母的员工:select * from emp where name regexp 'A|B';

正则查询(regexp):查询name中包含A或M字母打头的员工:select * from emp where name regexp '^[AM]';

正则查询(regexp):查询job为CLERK或MANAGER的员工:select * from emp where name regexp 'clerk|manager';

正则查询(regexp):查询memo中包含了数字的记录:select * from emp where memo regexp '[0-9]';

 

单字段排序:升序-asc可以省略 :select * from emp order by sal asc;   降序-desc不可以省略:select * from emp order by sal desc;

多字段排序:先按部门排序,每个部门再按薪水降序:select * from emp order by deptno,sal desc;

使用字段索引排序:按第一个字段的值来排序(从1开始):select * from emp order by 7 desc;

使用计算字段排序:按总输入(薪水+佣金)排序:1、先按佣金comm排序,2、说明null值,参与计算返回值仍为null,3、要用ifnull函数将空值转化为0

                                select name,comm,comm+1 comm2,sal+ifnull(comm,0) comm3 from emp;

                                select * from emp order by sal+ifnull(comm,0) desc;

标签:name,sal,数据库,改查,查询,emp,增删,where,select
来源: https://www.cnblogs.com/lc610-/p/16448604.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有