MySQL条件&&模糊&&去重查询.

目录

3. DQL:查询

3.2 条件查询

3.2.1 where

3.2.2 and 或 &&

3.2.3 or 或 ||

3.2.4 or 与 and 优先级

3.3.5 between... and ...

3.2.6 = 和 <> / !=

3.2.7 NULL/is not NULL

3.2.8 in() / not in ()  

 3.3 模糊查询(like)

3.4 去重查询


3. DQL:查询

查询是使用最多、最频繁的操作,因为前面的修改以及删除,一般会交给数据库专业的人员,对于非数据库专业人员来说,老板一般会放心的让你对数据库只进行查询操作;

3.2 条件查询
3.2.1 where

SQL语句:select 列名 from 表名 where 条件;

这些都是表达条件的符号; 

3.2.2 and 或 &&

推荐使用 and, 也可以使用 between... and ;

3.2.3 or 或 ||

推荐使用 or ;

3.2.4 or 与 and 优先级

比如 查询年龄在35以上和薪水大于6000或大于7000的员工信息:
select * from emp where age >35 and sal>6000 or sal>7000;

如果这样写,意思为  where (age >35 and sal>6000) or sal>7000

因此需要加括号改变优先级: where age >35 and (sal>6000 or sal>7000)

 所以对于优先级问题,先读懂题目,不确定的话就加括号;

3.3.5 between... and ...

between ... and ... 相当 >= and <= ,表示一个区间范围(闭区间);

支持 date,char, int 数据类型;

另外,数据必须从小到大输入,也就是and左边的数据要小于and右边的数据;

3.2.6 = 和 <> / !=

等于即数学运算上的 “ = ” ;

不等于推荐使用‘<>’ ;

3.2.7 is NULL/is not NULL

是否为空使用null或者is not null,不应该使用 等于/不等于 

3.2.8 in() / not in ()  

in 对应 or ;        not in 对应 and; 

举一个例子:

(1) where  job=' clerk ' or job= ' manager ' ;可以写为 where job in ('clerk' , ' maneger' ) ;

(2) where job <>' clerk ' and job <> ' manage ';可以写为 where job not in(‘clerk','manager');

 注意:in 忽略空 ;not in 不忽略空

(1) where job in(null, ' clerk ' ) 相当于where job =null or job ='clerk' ;

但是,job=null本身是错误的,应该是job is null,对于这种错误,SQL语句会忽略,还记得前面说过in 相当于 or 吗?;

where job in(null, ' clerk ' ) = where job='clerk' ;

(2) where job not in (null,'clerk') 相当于where job <> null and job<>' clerk ' ;

但是job<>null 是错误的,又因为关键词是 and,因此,没有查询结果;

 3.3 模糊查询(like)

通配符:    "_" 表示任意一个字符;"%" 表示0到多个任意字符;

比如查询所有姓李的员工的信息: select * from emp where name like '李%';

查询所有名字以s结尾的员工的信息:select * from emp where name like '%s';

查询所有名字中含有s的员工的信息:select * from emp where name like '%s%';

注意:如果查询字段中含有通配符的数据,必须使用转义字符:\;

比如查询名字中含有_的员工的所有信息 L:select * from emp where name like '%\_%';

3.4 去重查询

单一字段去重; 比如

select distinct name from emp;

多个字段去重:比如

select distinct name ,manage from emp; 将name和manage看做一个整体

distinct 必须放在所有字段的前面;

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值