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

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

相关推荐
葵续浅笑3 分钟前
LeetCode - 杨辉三角 / 二叉树的最大深度
java·数据结构·算法·leetcode
wdfk_prog10 分钟前
[Linux]学习笔记系列 -- [kernel][time]timer
linux·笔记·学习
装不满的克莱因瓶13 分钟前
【Java架构师】各个微服务之间有哪些调用方式?
java·开发语言·微服务·架构·dubbo·restful·springcloud
杨筱毅19 分钟前
【穿越Effective C++】条款13:以对象管理资源——RAII原则的基石
开发语言·c++·effective c++
N 年 后25 分钟前
cursor和传统idea的区别是什么?
java·人工智能·intellij-idea
serve the people28 分钟前
Prompt Serialization in LangChain
数据库·langchain·prompt
万事大吉CC28 分钟前
Win11卸载重装oracle 11g数据库
数据库
Wilber的技术分享34 分钟前
【大模型实战笔记 6】Prompt Engineering 提示词工程
人工智能·笔记·llm·prompt·大语言模型·提示词工程
JJJJ_iii36 分钟前
【机器学习16】连续状态空间、深度Q网络DQN、经验回放、探索与利用
人工智能·笔记·python·机器学习·强化学习
hmbbcsm36 分钟前
python学习之路(六)
学习