DQL( 数据查询语言)

1. 基本查询

select * from 表名;

select 字段,字段2,... from 表名;

select * from 表名 where 筛选条件;

select 字段,字段2,... from 表名 where 筛选条件;

2. 范围查询

复制代码
select * from emp where sal = 3000; 
select * from emp where sal != 3000;
select * from emp where sal >= 3000;
select * from emp where sal <= 3000;
select * from emp where sal > 3000;
select * from emp where sal < 3000;
select * from emp where sal <> 3000;   -- 不等于

3. 包含查询

复制代码
select * from emp where deptno in(10,20);

4. 闭区间查询

复制代码
select * from emp where sal between 950 and 2450;

5. 逻辑查询

逻辑查询 not>and>or and:遇假则假 or:遇真则真 not:取反

复制代码
and:遇假则假  or:遇真则真  not:取反
--查询emp表中20号部门,薪资大于2000的员工的信息
select * from emp where deptno=20 and sal>2000;
--查询emp表中,岗位是SALESMAN或者ANALYST的员工信息
select * from emp where job='SALESMAN' or job='ANALYST';
--查询不是老板的员工信息
select * from emp where not job='PRESIDENT';

6. 空值查询

is null 为空 is not null 不为空

复制代码
select * from emp where comm is null;
select * from emp where comm is not null;

7. 模糊查询

通配符:

_:代表这个位置只有1个字符;

%:代表这个位置有0个或者无数个字符;

复制代码
--查询emp表格员工的工种是以M开头的员工信息
select * from emp where job like'M%';
--查询员工姓名倒数第2个字母是E的员工信息
select * from emp where ename like'%E_';
  • 查询表格中带有%的数据 -- escape

需要把%或者_这个通配符转义称为普通的字符串

复制代码
--查询表格中带有%的数据
select * from op9 where n like'%\%%' escape'\';
--查询表格中带有_的数据
select * from op9 where n like'%\_%' escape'\';
相关推荐
青衫码上行5 小时前
【MySQL数据库 | 第五篇】DDL操作2
数据库·mysql
远方16096 小时前
43-Oracle 系统视图一览
数据库·sql·oracle·database
叶子椰汁7 小时前
ORMPP链接MySQL 8.0错误
服务器·数据库·c++·mysql
电商数据girl7 小时前
【经验分享】浅谈京东商品SKU接口的技术实现原理
java·开发语言·前端·数据库·经验分享·eclipse·json
老兵发新帖7 小时前
AWS RDS :多引擎托管数据库服务
数据库·云计算·aws
舒一笑7 小时前
MySQL层级查询实战:无函数实现部门父路径
mysql
liyongjie8 小时前
openEuler安装BenchmarkSQL
数据库
咚咚咚小柒8 小时前
SQL基础知识,MySQL学习(长期更新)
数据库·sql·mysql·database
四四夕夕9 小时前
【保姆级图文】1panel 面板部署雷池社区版:从环境搭建到安全验证全流程实录
数据库·安全
仍然探索未知中10 小时前
MySQL数据库介绍以及安装(本地windows、Ubuntu 20.04)
数据库·mysql