002-MYSQL练习-检索+通配符+查询

以下内容是学习《mysql必知必会》中的函数

排序检索

一、ORDER BY,根据某一列/多列来对输出进行排序

select bank_name from bank order by bank_name;

select * from deposite order by b_id,c_id;
// 先根据b_id排序,再根据c_id排序
//只有多个相同行,则根据第二个条件排序才有意义,否则后面的条件排序无法起到作用

二、指定排序方向

1、默认排序:A-Z的升序

降序:DESC

select * from deposite order by amount DESC;

2、结合多种排序

例如先按银行,再按照存款大小排

select * from deposite order by b_id, amount DESC;

注:所有不指定的都是以升序来进行排序,若所有的都进行降序排序,则需要每一列后面都加上DESC。

3、找出最大或者是最小值

select * from deposite order by amount DESC limit 1;//limit表示只返回一行

 

过滤数据

1、where字句

2、检查单个值

select * from bank where b_id='B0001';//检查b_id为B0001的数据

select * from deposite where amount<20000;//查找amount小于20000的数据信息
select * from deposite where amount<=26267;

3、不匹配检查

select * from deposite where b_id !='B0001';

4、范围值检查

select * from deposite where amount between 60000 and 300000;
//必须从小到大,闭区间

5、空值检查——不是值为0,而是没有值

select * from customer where location is null;

注:用不匹配!=——是无法筛选出NULL值的行,所以一定需要单独校验NULL

select * from customer where location !='南京';
//这个无法筛选出NULL值

 

数据过滤

1、多重条件

select * from customer where location !='南京' and salary>5000;
//每多一个条件,多加一个AND

2、并列条件

select * from customer where location='广州' or salary>6000; 
// OR 满足任一条件即可

3、顺序计算——AND的优先级比OR,哪怕顺序在后,也会先执行,要先执行OR,需要添加括号

select * from customer where salary>6000 or salary<4000 and location='南京';
//被识别为地址为南京的,金额小于4000的,或者是金额大于6000

select * from customer where (salary>=6000 or salary<4000) and location='南京';
//识别为,金额大于等于6000或者小于4000,且地址为南京

4、IN操作符——指定条件范围

select * from customer where location in ('广州','济南');
//等价于select * from customer where location='广州' or location='济南';

5、NOT——否定之后的所有条件

not 可以跟in, between, exists 语句,进行取反

select * from customer where location not in ('济南');

select * from customer where salary not between 1000 and 6000;

 

通配符过滤

1、%进行匹配

select * from customer where salary like '3%';

可前后都匹配

select * from deposite where amount like '%62%';

2、下划线匹配——只能匹配1个字符,跟%多个不同

3、算术计算

select c_id*amount as number from deposite where b_id='B0001';
//支持加减乘除

字段

1、拼接字段

Concat()——拼接串

 select concat (name, '(', c_id,')') from customer;

2、 AS 设置别名

select c_id as 客户编号,salary,name from customer;

数据处理

注:MYSQL的日期格式是:yyyy-mm-dd

select * from deposite where dep_date = '2011-04-05';

搜索日期范围

select * from deposite where Date(dep_date) between '2003-01-01' and '2007-12-31';
//DATE()是强制提取日期,避免保存的包含具体时间,无法查询出

具体的年月

select * from deposite where Year(dep_date) =2008 and Month(dep_date)=12;
//返回2008年12月的内容——YEAR()提取年,MONTH()提取月份


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值