您的位置:首页 > 数据库 > MySQL

MYSQL数据检索总结

2016-08-11 20:58 211 查看
用到的表是employee,建表语句如下(也可以自己建一个):

create table employee(
number varchar(20) primary key not null,
name varchar(20),
age int,
salary decimal(10,2)
);


插入的数据如下:

insert into employee(number,name,age,salary)
values('DEV001','Tom',25,8300),('DEV002','Jerry',28,2300.80),
('SALE001','John',23,5000),('SALE002','Kerry',28,6200),
('SALE003','Stone',22,1200),('HR001','Jane',23,2200.88),
('HR002','Tina',25,5200.36),('IT001','Smith',28,3900);


数据检索总结如下:

1.为列取别名

定义格式:列名 AS 别名 (AS可以省略)

例如:select FNumber as number from employee;
或 select FNumber number from employee;


2.按条件过滤

where 条件


3.数据汇总

常用的聚合函数(参数都是字段名):

1).max 计算字段最大值

例子:select max(salary) as max_salary from
employee where age > 25;

2).min 计算字段最小值

例子:select avg(age) as avg_age from employee where salary > 3800;

select min(salary),max(salary) from employee;

3).avg 计算字段平均值

例子:select avg(age) as avg_age from employee where salary > 3800;

4).sum 计算字段合计值

例子:select sum(salary) as total_salary from employee;

5).count统计数据条数

这个函数有两种用法,一个是count(*)统计的是结果集的总条数,一个是count(字段名)是统计结果集中该字段名不为空值的条数.


4.排序

语法:order by 字段名 ASC (升序,这是默认的可省略)
order by 字段名 DESC (降序)

例子:select * from employee order by age;

select * from employee order by age desc;

可以指定多个排序列
规则是:数据库系统会按照优先级进行处理,先按照第一条排序规则进行排序,如果第一个排序规则无法区分两条记录的顺序则按照第二条排序规则进行排序,依次类推.
还要注意的是,order by语句必须在where语句之后。

例子:select * from employee order by age desc,salary desc;


5.高级数据过滤

通配符过滤需要使用like关键字
1).单字符匹配

利用_表示一个任意字符,比如a_b,可以匹配到acb adb等等._还可以出现多次,__a__b__,这里一共有6个任意字符.

例子:select * from employee where name like '_erry';

select * from employee where name like '__n_';

2).多字符匹配

利用%可以匹配任意多个字符(0或N个)
单字符和多字符匹配可以一起使用.
例子:select * from employee where name like 'T%';

select * from employee where name like '%n_';

3).集合匹配

这个功能只在MSSQLServer上支持,但是可以利用通配符加逻辑符实现

首先集合匹配的用法为:[SJ] 可以匹配 S 或 J

[^SJ] 可以匹配非S 或 非 J

例子:select * from employee where name like '[SJ]%';
等价于
select * from employe where name like 'S%' or name like 'J%';

select * from employee where name like '[^SJ]%'
等价于
select * from employee where not (name like 'S%')
and not(name like 'J%');

4).空值检测

不能使用普通的等于运算符进行判断,而要使用is null关键字,不为空则使用 is not null

用法:待检测字段名 is null

例子:select * from employee where name is null;
select * from employee where name is not null;

5).反义运算符

!表示反义,例如 != 不等于 !< 不小于 !>不大于
注意只有MSSQLServer和DB2数据库上支持,可以用同义词变通,例如不小于就是大于等于,SQL提供了通用的不等于运算符<>.

例子:select * from employee where age!=22 and salary!<2000;
等价于
select * from employee where age<>22 and salary >=2000;
等价于
select * from employee where not(age=22) and not(salary <2000);

6).多值检测

可以使用or语句来连接多个等于判断或者使用in语句

例子:select age,number,name from employee where
age=23 or age=25 or age=28;

select age,number,name from employee where
age in(23,25,28);

7).范围值检测

可以使用> >= < <=来实现,也可以使用SQL提供从范围检测语句实现
字段名 bettween 左值范围 and 右值范围 (闭区间)
例子:select * from employee where age>=23 and age<=27;
等价于
select * from employee where age between 23 and 27;

8).数组分组

数据分组用来将数据分为多个逻辑组,从而可以对每个组进行聚合运算。且分组语句必须和聚合函数一起使用

用法:group by 分组字段

group by 子句中可以指定多个列,然后数据库进行逐层分组。意思是先按照第一个字段分组,然后再对每个组再进行第二个字段的分组,从而实现组中组.

例子:将年龄分组,统计每个年龄组的人数和平均工资
select age,count(*) as countofthisage,avg(salary) as avg_salary  from employee group by age;

还可以用having语句进行组的过滤

例子:select age,count(*) from employee group by age having count(*)>1
意思是一个组的记录要大于1

9).限制结果集行数

limit 首行行号,要返回的结果集最大数

例子:select * from employee order by salary desc limit 2,5;

10).抑制数据重复

distinct关键字是用来进行重复数据抑制的,它是对整个结果集进行数据重复抑制的,
而不是针对每一个列

例子:select distinct depatment from employee;

11).常量字段

例子:select 'CowNew集团' as CopanyName,name age,subcompany from employee;

CowNew集团就是常量字段,它不是真实存在的.

12).字段间计算

例子:select number,name,age*salary from employee;

字段间可以做运算,不管是常量字段还是普通字段,两两之间都能做运算。

13).数据处理函数

除了聚合函数之外,还有数学函数、日期处理函数、字符串处理函数.
常用的有length、substring、sin、abs
例子:select name,length(name) as nameLength from    employee where name is not null;

select name,substring(name,2,3) from employee where name is not null;

select name,age,sin(age),abs(sin(age)) from employee;

14).字符串拼接

字符串拼接要使用concat,如果使用“+”的话,SQL会自动把两边转换成数字进行相加运算,转换失败则为0.

例子:select concat('工号为:',number,'的员工的幸福指数:',salary/(age-21)) from employee;

concat支持只有一个参数的用法,这时concat可以看作是一个将这个参数值尝试转化为字符串类型值的函数.

concat_ws函数可以在待拼接的字符串之间加入指定的分隔符,它的第一个参数为采用的分隔符,而剩下的参数则为待拼接的字符串.

例子:select concat_ws(',',number,age,depatment,salary) from employee;

15).联合结果集

使用union运算符来将两个或多个查询结果集联合为一个结果集中
基本原则:每个结果集必须有相同的列数,每个结果集的列必须类型相同.

例子:select number,name,age from employee union
select idCardNumber,name,age from tempemployee;

默认情况下,union运算符合并了两个查询结果集,其中完全重复的数据行被合并为了一条。使用union all可以避免这种情况.

例子:select name,age from employee union all
select name,age from tempemployee;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: