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'\';
相关推荐
AI 嗯啦3 分钟前
SQL详细语法教程(四)约束和多表查询
数据库·人工智能·sql
阿里云大数据AI技术5 分钟前
【跨国数仓迁移最佳实践6】MaxCompute SQL语法及函数功能增强,10万条SQL转写顺利迁移
python·sql
杜子不疼.23 分钟前
《Python学习之文件操作:从入门到精通》
数据库·python·学习
TDengine (老段)1 小时前
TDengine IDMP 高级功能(4. 元素引用)
大数据·数据库·人工智能·物联网·数据分析·时序数据库·tdengine
DashVector1 小时前
如何通过Java SDK分组检索Doc
java·数据库·面试
Olrookie2 小时前
XXL-JOB GLUE模式动态数据源实践:Spring AOP + MyBatis 解耦多库查询
java·数据库·spring boot
苏婳6662 小时前
【最新版】怎么下载mysqlclient并成功安装?
数据库·python·mysql
Tapdata4 小时前
《实时分析市场报告 2025》上线 | 从批处理到实时洞察,2025 年全球实时分析市场全景解读
数据库
海梨花4 小时前
【从零开始学习Redis】项目实战-黑马点评D2
java·数据库·redis·后端·缓存
Java水解4 小时前
MySQL 亿级数据表平滑分表实践:基于时间分片的架构演进
后端·mysql