dw做的网站如何发布,百度浏览器打开,网站 建设目标,金色网站模板文章目录1.基础查询2.条件查询3.排序查询4.常见函数4.1 单行函数4.1.1 字符函数4.1.2 数学函数4.1.3 日期函数4.1.4 流程控制函数4.1.5 其他函数4.2 分组函数/统计函数/聚合函数5.分组查询1.基础查询
语法#xff1a;
SELECT 要查询的东西
【FROM 表名】;#查询employees表中所…
文章目录1.基础查询2.条件查询3.排序查询4.常见函数4.1 单行函数4.1.1 字符函数4.1.2 数学函数4.1.3 日期函数4.1.4 流程控制函数4.1.5 其他函数4.2 分组函数/统计函数/聚合函数5.分组查询1.基础查询
语法
SELECT 要查询的东西
【FROM 表名】;#查询employees表中所有字段
select * from employees;#查询employees表中的first_name字段
select first_name from employees;#查询employees表中的多个字段
select first_name,salary FROM employees;#查询常量值
select 100123;
select zhaoxr#查询表达式
select 100*98;#查询函数
select version();#起别名
select 98*98 as result;
select last_name as 姓,first_name as 名 from employees;
select last_name 姓,first_name 名 from employees;#去重
select distinct department_id from employees;# 的作用
select 10099;
select 12399;
select zhaoxrlinux;
select null16;# concat函数
select concat(last_name, ,first_name) 姓名 from employees;类似于Java中 :System.out.println(要打印的东西);
特点
① 通过select查询完的结果 是一个虚拟的表格不是真实存在
② 要查询的东西 可以是常量值、可以是表达式、可以是字段、可以是函数2.条件查询
条件查询根据条件过滤原始表的数据查询到想要的数据
语法
select 要查询的字段|表达式|常量值|函数
from 表
where 条件 ;分类
一、条件表达式示例salary10000条件运算符 ! 二、逻辑表达式
示例salary10000 salary20000逻辑运算符and:两个条件如果同时成立结果为true否则为falseor(||)两个条件只要有一个成立结果为true否则为falsenot(!)如果条件成立则not后为false否则为true三、模糊查询
like
between and
in
is null | is not null通配符
%任意多个字符包含0个字符
_单个字符
\转义字符\_代表字符_示例last_name like a%# 查询工资大于等于12000的员工ID,姓名和薪水
select employee_id,CONCAT(last_name, ,first_name) as 姓名,salary
from employees
where salary12000;# 查询部分编号不等于90的员工姓名和部门编号
select CONCAT(last_name, ,first_name) as 姓名,department_id
from employees
where department_id!90;#查询工资在10000到20000之间的员工ID姓名和工资
select employee_id,concat(last_name, ,first_name) as 姓名,salary
from employees
where salary10000 and salary20000;#查询员工姓中含有a的员工信息
select *
from employees
where last_name like %a%;#查询员工姓第三个字母是l,第五个是e的员工信息
select *
from employees
where last_name like __l_e%; #查询员工姓的第二个字符是_的员工信息
select *
from employees
where last_name like _\_%;#查询员工ID在100到200之间的员工信息,包含100和200
select *
from employees
where employee_id between 100 and 200;#查询工种编号是IT_PROG,PU_CLERK,ST_CLERK的员工信息
select *
from employees
where job_id in(IT_PROG,PU_CLERK,ST_CLERK);#查询奖金率为null的员工信息
select *
from employees
where commission_pct is null;3.排序查询
语法
select要查询的东西
from表
where 条件order by 排序的字段|表达式|函数|别名 【asc|desc】#查询员工信息工资由低到高排列
select *
from employees
order by salary asc;#查询员工信息部门编号大于等于90并且按照入职先后排列
select *
from employees
where department_id90
order by hiredate asc;#查询员工信息和年薪并按照年薪从低到高排列
select *,salary*12*(1ifnull(commission_pct,0)) as 年薪
from employees
order by salary*12*(1ifnull(commission_pct,0)) asc;#查询员工的姓名和工资按照姓名的长度从小到大排列
select CONCAT(last_name, ,first_name) as 姓名,length(concat(last_name, ,first_name)) as 姓名长度,salary
from employees
order by length(concat(last_name, ,first_name)) asc;#查询员工信息先按工资从低到高排名再按员工编号从低到高排序
select *
from employees
order by salary asc,employee_id asc;4.常见函数
4.1 单行函数
4.1.1 字符函数 concat拼接substr截取子串upper转换成大写lower转换成小写trim去前后指定的空格和字符ltrim去左边空格rtrim去右边空格replace替换lpad左填充rpad右填充instr返回子串第一次出现的索引length 获取字节个数#length函数|查询员工的姓以及姓的长度
select last_name,length(last_name)
from employees;#concat函数|查询员工的姓名
select concat(last_name, ,first_name) as 姓名
from employees;#upper函数和lower函数|查询员工的姓名姓大写名小写
select concat(upper(last_name), ,lower(first_name)) as 姓名
from employees;#substr截取函数|注意截取函数从1开始
#返回i love china中的china
select substr(i love china,8);#返回我爱你中国中中国
select substr(我爱你中国,4);#返回i love china中的love
select substr(i love china,3,4);#instr返回子串第一次出现的索引|返回i love china中的c在第几个位置
select instr(i love china,c);#trim去空格|删除 love 左右的空格
select trim( love );#trim去除指定的字符|删除aaaaaloveaaaa左右的a
select trim(a from aaaaaaaloveaaaaa);#lpad填充|使用0填充字符我爱你,总字符数10个填充结果为0000000我爱你
select lpad(我爱你,10,0);#replace替换|将我爱china,替换为我爱中国
select replace(我爱china,china,中国);4.1.2 数学函数 round 四舍五入rand 随机数floor向下取整ceil向上取整mod取余truncate截断#数学函数
#round四舍五入|2.55四舍五入保留整数部分
select round(2.55);#ceil向上取整返回该数的最小整数|取整2.0001,2.0000
select ceil(2.0001),ceil(2.0000);#floor向下取整返回该数的最大整数|取整-2.0001,2.0000
select floor(-2.0001),floor(2.0000);#truncate截断保留小数几位|1.99999保留两位小数
select truncate(1.99999,2);#mod取余|10/3的余数
select mod(10,3);4.1.3 日期函数 now当前系统日期时间curdate当前系统日期curtime当前系统时间str_to_date 将字符转换成日期date_format将日期转换成字符#时间函数
#查询当前时间
select now();#获取指定的年月日获取当前年份,月份,日
select year(now()) as 年,month(now()) as 月,day(now()) as 日;#获取员工姓名和入职年份
select concat(last_name, ,first_name) as 姓名,year(hiredate) as 入职年份
from employees;#str_to_date将日期格式的字符转换为规定的日期格式规定的日期格式2021-04-29
select str_to_date(2021年4月29日,%Y年%m月%d日),str_to_date(04/29/2021,%m/%d/%Y);#date_format将日期转换为自己想要的日期字符|查询员工的姓名和入职时间入职时间显示为2021年4月29日的格式
select concat(last_name, ,first_name) as 姓名,date_format(hiredate,%Y年%m月%d日) as 日期
from employees;4.1.4 流程控制函数 if 处理双分支if(判断条件,真就执行此处假就执行此处)case 要判断的字段或者表达式when 常量1 then 要显示的值1或者语句1when 常量2 then 要显示的值2或者语句2。。。。。else 要显示的值n或者语句nendcase when 条件1 then 要显示的值1或者语句1when 条件2 then 要显示的值2或者语句2。。。。。else 要显示的值n或者语句nend#流程控制函数
#if函数|查询员工的姓奖金率如果有奖金返回有奖金没有奖金返回没奖金
select last_name,commission_pct,if(commission_pct is null,没奖金,有奖金) as 奖金
from employees;#case|查询员工的姓名工资部门ID#如果部门90salary*1.1;如果部门100salary*1.2;如果部门110salary*1.3;其它salary不变;并显示
select concat(last_name, ,first_name),department_id,salary as 原始工资,case department_idwhen 90 then salary*1.1when 100 then salary*1.2when 110 then salary*1.3else salaryend as 当前工资
from employees;#case|查询员工的姓名工资#如果工资小于10000显示低工资#如果工资大于等于10000小于20000显示中等工资#如果工资大于等于20000显示高工资
select concat(last_name, ,first_name),salary,case when salary10000 then 低工资when salary10000 and salary20000 then 中等工资when salary20000 then 高工资end as 工资区间
from employees;4.1.5 其他函数 version版本database当前库user当前连接用户#其它函数
#version查看版本号
select version();#database查看当前数据库
select database();#user查看当前用户
select user();4.2 分组函数/统计函数/聚合函数 sum 求和max 最大值min 最小值avg 平均值count 计数#分组函数
#简单使用
select sum(salary) as 总工资,max(salary) as 最高工资,min(salary) as 最低工资,avg(salary) as 平均工资,count(salary) as 工资个数
from employees;#查询有多少中工资
select count(distinct salary)
from employees;#查询employees表有多少行
select count(*) as 行数,count(1) as 行数
from employees;特点1、以上五个分组函数都忽略null值除了count(*)2、sum和avg一般用于处理数值型max、min、count可以处理任何数据类型3、都可以搭配distinct使用用于统计去重后的结果4、count的参数可以支持字段、*、常量值一般放1建议使用 count(*)5.分组查询
语法
select 查询的字段分组函数
from 表
group by 分组的字段特点
1、可以按单个字段分组
2、和分组函数一同查询的字段最好是分组后的字段
3、分组筛选针对的表 位置 关键字
分组前筛选 原始表 group by的前面 where
分组后筛选 分组后的结果集 group by的后面 having4、可以按多个字段分组字段之间用逗号隔开
5、可以支持排序
6、having后可以支持别名#查询每个部门的平均工资
select avg(salary),department_id
from employees
group by department_id;#查询每个工种的最高工资
select max(salary),job_id
from employees
group by job_id;#查询工资大于10000的每个工种的最高工资
select max(salary),job_id
from employees
where salary10000
group by job_id;#查询哪个部门员工的个数大于2
select count(*),department_id
from employees
group by department_id
HAVING count(*)2;#按员工的姓名长度分组查询每一组员工的个数筛选员工个数大于5的有哪些
select count(*),length(last_name) as len_name
from employees
group by length(last_name)
having count(*)5;#查询每个部门每个工种的平均工资
select avg(salary),department_id,job_id
from employees
group by department_id,job_id;#查询每个部门每个工种的平均工资,并按照降序排列
select avg(salary),department_id,job_id
from employees
group by department_id,job_id
order by avg(salary) desc;