MySQL查询语句(DQL)

文章目录

查询语句(DQL)

简单查询

\c可以清空命令

查一个字段

sql 复制代码
select 字段名 from 表名

字段名和表名属于标识符,按照表的实际情况填写,不知道字段名的,可以使用desc命令查看表结构



查多个字段

sql 复制代码
select 字段名,字段名..... from emp;

就是加逗号就好了

字段的前后顺序,会导致显示的顺序改变



查所有字段

sql 复制代码
select * from 表名

这个SQL语句不在项目编码中使用,因为效率比较低



查询字段可以进行数学运算

sql 复制代码
select ename, (sal+100) * 12 from emp;


查询时字段可起别名

sql 复制代码
select 字段名 as 别名 from 表名

别名可以是中文,但是低版本mysql不写,并且要用单引号引起来


省略 as 关键字,只用空格也可以


别名中有空格必须要用 单引号 括起来,不然报错




条件查询

条件 说明
= 等于
<>或!= 不等于
>= 大于等于
<= 小于等于
> 大于
< 小于
between...and... 等同于 >= and <=
is null 为空
is not null 不为空
<=> 安全等于(可读性差,很少使用了)。
and 或 && 并且
or 或 || 或者
in 在指定的值当中
not in 不在指定的值当中
exists
not exists
like 模糊查询
sql 复制代码
select 
  ...
from
  ...
where
  过滤条件;

执行顺序

from -> where -> select

大于等于这些都比较无脑,直接 and 开始



and (&&)

and 表示并且,用 && 也行,java 的这个一样

sql 复制代码
select ENAME, sal from emp where sal > 2000 and sal < 3000;

从 emp 中找到 sal > 2000 并且 sal < 3000 的 ENAME, SAL



or (||)

or 表示并且,用 || 也行,java 的这个一样

sql 复制代码
select ename, job from emp where JOB = 'CLERK' or JOB = 'MANAGER';

从 emp 中找 到 job = clerk 或者 job = manager 的 ename, job

and和or同时出现时,and优先级较高,会先执行,如果希望or先执行,这个时候需要给or条件添加小括号。



between...and...

between...and...等同于 >= and <=

【a,b】闭区间

返回左小右大

sql 复制代码
select ename, sal from emp where sal between 2000 and 3000;

从 emp 中 找到 sal 在 [2000, 3000] 的 ename, sal

日期要加单引号



is null 和 is not null

判断某个数据是否为null,不能使用等号,只能使用 is null。

判断某个数据是否不为null,不能使用不等号,只能使用 is not null

不然返回 false


is null

sql 复制代码
select ename, comm from emp where comm is null

从 emp 中找到 comm 为 null 的 ename 和 comm


is not null

sql 复制代码
select ename, comm from emp where comm is not null;

从 emp 中找到 comm 不为 null 的 ename 和 comm




in 和 not in

in 的意思是这有这几个值,而不是区间比如:sal in(1500, 5000) 表示 sal 是 1500 和 5000


in

sql 复制代码
select ename, comm from emp where comm in(300, 500);

从 emp 中找到 comm 为 300, 500 的 ename 和 comm


not in

sql 复制代码
select ename, comm from emp where comm not in(300, 500);

从 emp 中找到 comm 不是 300, 500 的 ename 和 comm


和 NULL 一起可能出现的问题

in

sql 复制代码
select * from emp where comm in(NULL, 300);
//和上面等价
select * from emp where comm = NULL or comm = 300;

comm == NULL 肯定是 false,因为判断 NULL 要用 is null,但是这是 or 不影响后面,自动过滤掉了

not in

sql 复制代码
select * from emp where comm not in(NULL, 300);
//和上面等价
select * from emp where comm <> null and comm <> 300;

comm <> null 肯定是false, 因为判断 NULL 要用 is not null,但是这里是 and 前面 false 所以没必要运算了 直接 300 的都查不到了

not in是不会自动忽略NULL的,所以在使用not in的时候一定要提前过滤掉NULL。




like (模糊查询)

sql 复制代码
select .. from .. where 字段 like '通配符表达式';

在模糊查询中,通配符主要包括两个:一个是%,一个是下划线_。其中%代表任意多个字符。下划线_代表任意一个字符。

sql 复制代码
 select ename from emp where ename like '_A%';

从 emp 中 找到 ename 为 第二个字符为 A 的 ename

如果有和通配符冲突的字符就用"\"转意字符

相关推荐
小光学长4 分钟前
基于vue框架的电信用户业务管理系统的设计与实现8ly70(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
张槊哲19 分钟前
函数的定义与使用(python)
开发语言·python
程序员不想YY啊19 分钟前
MySQL元数据库完全指南:探秘数据背后的数据
数据库·mysql·oracle
数据最前线23 分钟前
Doris表设计与分区策略:让海量数据管理更高效
数据库
iuyou️25 分钟前
Spring Boot知识点详解
java·spring boot·后端
北辰浮光27 分钟前
[Mybatis-plus]
java·开发语言·mybatis
时光追逐者32 分钟前
MongoDB从入门到实战之MongoDB快速入门(附带学习路线图)
数据库·学习·mongodb
一弓虽37 分钟前
SpringBoot 学习
java·spring boot·后端·学习
南客先生41 分钟前
互联网大厂Java面试:RocketMQ、RabbitMQ与Kafka的深度解析
java·面试·kafka·rabbitmq·rocketmq·消息中间件
ai大佬44 分钟前
Java 开发玩转 MCP:从 Claude 自动化到 Spring AI Alibaba 生态整合
java·spring·自动化·api中转·apikey