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

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

相关推荐
He BianGu2 分钟前
笔记:简要介绍WPF中FormattedText是什么,主要有什么功能
笔记·c#·wpf
疑惑的杰瑞7 分钟前
[C语言]连子棋游戏
c语言·开发语言·游戏
wrx繁星点点7 分钟前
创建型模式-单例模式
java·开发语言·单例模式
冷眼看人间恩怨11 分钟前
【话题讨论】AI时代程序员核心力:技术深耕,跨界学习,软硬兼备
人工智能·学习·aigc
Data 31713 分钟前
经典sql题(二)求连续登录最多天数用户
大数据·数据库·数据仓库·sql·mysql
杨~friendship23 分钟前
Ubuntu上使用qt和opencv显示图像
linux·开发语言·c++·qt·opencv·ubuntu
街 三 仔27 分钟前
【C语言零基础入门篇 - 3】:格式化输入输出、字符操作和sizeof运算符揭秘
c语言·开发语言
guangzhi063329 分钟前
JAVA执行引擎详细介绍
java·jvm
服装学院的IT男32 分钟前
【Android 13源码分析】WindowContainer窗口层级-3-实例分析
android·数据库
喜欢猪猪42 分钟前
TCP/IP网络编程概念及Java实现TCP/IP通讯Demo
开发语言·php