当前位置: 移动技术网 > IT编程>数据库>Mysql > Mysql--select基础查询

Mysql--select基础查询

2018年08月20日  | 移动技术网IT编程  | 我要评论

校花我爱你下载,地理地图,申纪兰被爆料全家都是高官巨富

基本语法:select 查询列表 from 表名
查询列表可以是表中字段、常量值、表达式、函数;查询的结果是一个虚拟的表格。
注意:
①sql语言大小写不敏感
②关键字不能分行或略写
③一般书写方式为换行缩进
一、基础查询
1.经典查询(查询表中的字段)
①查询单个字段
select 字段1 from 表1;
②查询多个字段
select 字段1,字段2, . . .字段n from表1;
③查询所有字段(用“”表示所有字段或者用枚举所有的字段)
select
from 表1;
select 字段1,字段2,字段3,...,字段last from 表1;
2.查询常量值
select 520;
select ‘chenzui’;
3.查询表达式
select 100/20;
4.查询函数
select version();
5.起别名
①方式1用as
select 100/20 as “结果”;
②方式2 用” ”空格
select 100/20 结果;
6.去重distinct
select distinct department_id from employees;
7.mysql中+的功能(运算符)
①两个都是数值型,做加法运算。select 200+100;返回300。
②一个是字符型一个是数值型,将字符型转换为数值型,若能转换成功,则继续运算,转换失败,将字符型转换为0,继续运算。
select “120”+120;返回240;select “120aaa”+120;返回240;select “aaa120”+120;返回120。
③一方是null,结果返回null。select null+100;返回null。
二、条件查询
语法:select 查询列表 from 表名 where 筛序条件;
1.按条件表达式筛选
简单条件运算符:>、<、=、 !=、<>、>=、<=
案例1 查询工资大于1000的员工信息
select * from employees where salary>1000;
案例2 查询部门编号等于80,的员工名字和部门id
select last_name,department_id from employees where department_id = 80;
2.逻辑表达式筛选
逻辑运算符
作用:用于连接条件表达式 && || ! and or not
and 或 && 两个条件都为true,结果为true,否者返回false
or 或 || 只有一个条件为true,结果为true,否者为false
not 或 ! 连接条件本身为true,结果为false,否者为true
案例 查询工资在10000-15000的员工名字和工资
select last_name , salary from employees where salary>10000 and salary<15000;
3.模糊查询
like、between and 、in、is null
①like 一般和通配符搭配使用
通配符:‘%’任意多个,包括0个,’_’任意单个。
案例1 查询名字中包含”c”的员工信息
select * from employees where last_name like “%c%”;
案例2 查询名字以a开头第4个字母为z的员工信息
select * from employees where last_name like “a__z%”;
案例3 查询第二个字符为””的员工名字
select last_name from employees where laset_name like “
$_%”;
②between and 可以使sql语句更简洁,包含临界值,两个临界值不能调换顺序
案例1 查询员工编号在150-200之间的员工信息
select * from employees where employee_id between 150 and 120;
select * from employees where employee_id >=150 and employee_id <=120;
③in 判断某段的值是否属于in列表中的某一项,提高了代码的简洁度,in列表的值必须一致或者兼容,in列表不支持通配符。
案例1 获取工种编号为aaa、bbb、ccc中一个的员工信息
select * from employees where job_id in(“aaa”,’bbb’,’’ccc);
select * from employees where job_id=”aaa” or job_id = “bbb” or job_id = “ccc”;
④=或者<>不能判断null值,is null和is not null 可以用于判断null值
案例1 查询没有奖金的员工信息
select * from employees where commission_pct is null;
⑤<=>安全等于
查询工资为10000的员工名字
select last_name from employees where salary <=>10000;
⑥is null 和 <=>
is null:只可以判断null值,可读性较高
<=>:既可以判断null值,又可以判断普通数值,可读性较差。
三、常见函数
1、单行函数
①字符函数:length(获取字节个数)、concat(拼接字符串)、upper(转化成大写)、lower(转换为小写)、substring、instr(返回字符串第一次出现的索引,不存在返回0)、trim(去除前后空格)、lpad(用指定字符实现左填充)、rpad(用指定的字符实现右填充)、replace(替换)

②数学函数
round():四舍五入
ceil():向上取整
floor():向下取整
truncate():截断
mod():取余,mod(a,b) : a-a/bb

③日期函数
now():返回当前日期加时间
curdate():返回当前日期
curtime():返回当前时间
str_to_date(字符类型,日期类型): 将字符类型转换为日期类型
date_format(日期类型,字符类型): 将日期类型转换为字符类型;

④流程控制函数
if函数:其效果类似于if else

case函数用法一[类似于switch用法]
语法:
case 要判断的字段或者表达式
when 常量1 then 要显示的值1或语句1;
when 常量2 then 要显示的值2或语句2;
when 常量3 then 要显示的值3或语句3;

else 要显示的值n或者语句n;
end

case函数用法二[类似于多重if]
语法:
case
when 条件1 then 要显示的值1或语句1
when 条件2 then 要显示的值2或语句2

else 要显示的值n或语句n
end

⑤其他函数
select version();
select database();
select user();
2、分组函数(聚合函数、统计函数、组函数)
分类:
求和(sum)、avg(平均值)、max(最大值)、min(最小值)、count(计算个数)
特点:
①sum和avg一般处理数值型,max、min、count又可以处理任何类型
②分组函数都忽略null值
③可以和distinct搭配实现去重运算
④count(
)用于统计数量
⑤和分组函数一同查询的字段要求是group by后的字段
案例1
select sum(salary) avg(salary) min(salary) max(salary) count(salary) from employees;
案例2 函数的适用性
select sum(last_name) ,avg(last_name) from employees; 返回值是0,0
案例3 忽略null值
select
sum(commission_pct) ,avg(commission_pct),sum(commission_pct)/35,sum(commission_pct)/107 from employees;
案例4 和distinct结合使用 会自动去重
select sum(distinct salary),sum(salary) from employees;
案例5 count()的使用
select count(salary) from employees;
select count(
) from employees;
select count(1) from employees;
案例6 和分组函数一同查询的字段有限制
select avg(salary),employee_id from employees;
四、排序查询
语法:select 查询列表 from 表名 [where 筛选条件] order by 排序的字段或者表达式
1.排序函数的用法
①asc表示升序,可以省略,desc是降序
②order by可以支持单个字段、别名、表达式、函数、多个字段
③order by 子句在查询语句最后面,除了limit子句
2.案例
select * from employees order by salary desc;
select * from employees where department_id >= 100 order by department_id;
select last_name , salary12(1+ifnull(commission_pct,0)) from employees order by salary12(1+ifnull(commission_pct,0));
select last_name , salary12(1+ifnull(commission_pct,0))as "年薪" from employees order by '年薪';
select department_id,avg(salary) from employees group by department_id order by avg(salary);
select last_name,salary from employees order by last_name desc ,salary asc;
五、group by
语法:select 字段1,字段2,… from 表名 [where 筛选条件] group by 字段1,字段2,..[having 筛选条件 limit 分页条件];
查询列表必须来自分组的字段,保证逻辑的一致性。
使用方式:
①group by 和聚合函数配合使用得时候,分组后计算
select avg(salary),last_name from employees group by department_id;
②与having配合使用得时候,分组后过滤;
select department_id,min(salary) from employees group by department_id having salary>3000;
③group by中聚合函数同时和非聚合函数一起使用时,非聚合函数取第一个匹配到时的字段内容。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网