MySQL查询语句(DQL)

查询语句(DQL)

简单查询

\c可以清空命令

查一个字段

select 字段名 from 表名

在这里插入图片描述

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



查多个字段

select 字段名,字段名..... from emp;

就是加逗号就好了

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

在这里插入图片描述



查所有字段

select * from 表名

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

在这里插入图片描述



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

select ename, (sal+100) * 12 from emp;

在这里插入图片描述



查询时字段可起别名

select 字段名 as 别名 from 表名

在这里插入图片描述

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


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

在这里插入图片描述


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

在这里插入图片描述




条件查询

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

执行顺序

from -> where -> select

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



and (&&)

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

select ENAME, sal from emp where sal > 2000 and sal < 3000;

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

在这里插入图片描述



or (||)

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

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】闭区间

返回左小右大

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

select ename, comm from emp where comm is null

在这里插入图片描述

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


is not null

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

select ename, comm from emp where comm in(300, 500);

在这里插入图片描述

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


not in

select ename, comm from emp where comm not in(300, 500);

在这里插入图片描述

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


和 NULL 一起可能出现的问题

in

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

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 (模糊查询)

select .. from .. where 字段 like '通配符表达式';

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

 select ename from emp where ename like '_A%';

在这里插入图片描述

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

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

  • 26
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值