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

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

相关推荐
杨杨杨大侠5 分钟前
Java 不再需要 JVM?一文看懂 GraalVM Native Image、Substrate VM 与 JDK
java·jvm·java ee
迷茫的大专生11 分钟前
高可用总结
redis·mysql·nginx·高可用
Sylvia33.18 分钟前
火星数据体育API|一站式接入足球篮球电竞等18+项目实时数据
java·开发语言·python·websocket·游戏
李少兄24 分钟前
解决 MySQL Lock wait timeout exceeded 报错
java
va学弟25 分钟前
Java 语言特性:泛型(Generics)
java·泛型
老纪的技术唠嗑局26 分钟前
Agent 习惯性删库跑路,数据库纷纷学 Git 续命
数据库·人工智能
yangmu320336 分钟前
从显像管到Micro-LED:屏幕的光之进化
学习·显示器
Data_Journal44 分钟前
使用 AutoScraper 进行网页抓取:分步教程
大数据·开发语言·数据库·python·scrapy
yume_sibai1 小时前
12-Rust 性能优化指南(性能分析 + 内存优化 + SIMD + 并发优化 + 编译器优化 + 基准测试)
开发语言·性能优化·rust
SL_staff1 小时前
插单风险的数据闭环治理:JVS-BI 在制造业实时预警中的技术实践
java·数据分析·数据可视化