mysql-5.单表查询

5.单表查询

select 表头,表头 as 别名 ,表头(+-*/的运算) from table_a

1.条件查询

where + 条件
<> , != 不等于
= 等于,也可以表示字符串值相等
<,> 小于,大于
>=,<= 大于等于,小于等于
between... and.. 两个值之间(左小右大,左右都闭),等同于 >=and <=
is null,is not null 为空,不为空 (注意null 不能使用=进行衡量)
and 并且,and 语句优先级高于or
or 或者 例:select name from table where name='张三'or ''李四'
not not 表示取非, 可以在is ,in (in 后是具体值,不是区间)中使用
and ,or的优先级对比

and 优先级高于or,若希望先执行or 可以使用(),不确定优先,注意使用()

例:查询工资大于5500,并且编号为10或20的员工

select  
	 name
from  
	emp
where  sal>5500 and (deptno=10 or deptno=20)
-- 这里and 优先级高于or ,括号的使用可以对内容进行组合,由此实现内查询
模糊查询

like 模糊查询,支持%和_(下划线)

  • %匹配任意一个字符
  • _代表任意一个字符(一个下划线占一个位置)
  • 若要like 含有_的内容,可以使用进行转义
- 表示以a开头的所有字符
表头  like 'a%'

- 表示以a结尾的所有字符
表头 like '%a'

- 表示表中含有_字符的所有字符
表头 like '%\_%'

- 表示第二字母是a的所有字符
 表头 like '_a%'

- 表示倒数第二字母是a的所有字符
 表头 like '%a_'

2、排序

order by 表头

  • +desc(指定降序)/asc(升序,默认)
  • +列号 如2
  • 排序总是在最后
  • slect ... from ... where .....order by
-- 多个字段排序  当字段相等才按后面进行排序
select sal from  emp order by sal desc,ename asc;

-- 了解 列号排序
select sal from emp order by 2

3.数据处理函数

  • 又称为单行处理函数
  • 单行处理函数的特征:一个输入对应一个输出
  • 多行处理函数:多行输入对应一个输出
  • 常见函数 select function(表头 ) from table_a 或者 在where 条件中
lower 转换小写
upper 转换大写
substr 取子串(substr(被截取的字符串,起始下标(注下标 从1开始),截取的长度))
length 取长度
trim 去空格
str_to_data 将字符串转换成日期
data_format 格式化日期
format 设置千分位format(表头,生成小数位数)
round 四舍五入 round(表头,保留的小数位)
rand() 生成随机数
ifnull 将null 转换成一个具体值 ;因为在所有数据中只要有null参与的数学运算结果就是null; 如:ifnull(表头,0) 如果为null 当做一个值来看
case... when..then..when..then.. else.....end case 表头 when 条件1 then 执行a when 条件2 then 执行2 else 其他 end
-- substr应用
-- 从 emp 获取以A开头的ename
select ename from emp where substr(ename,1,1)='A';

--  length应用
-- 查看emp中ename的字符长度
select length(ename) from emp ;


-- null 参与数据计算最终结果一定为null,为了避免这个现象,需要使用ifnull 函数
ifnull(表头,0) 如果表头为null 则视为0
select ename,sal+ifnull(comm,0)as salcomm from emp

--case ...when. then... when ... then..else... end 应用
-- 当员工的岗位为sd时,工资上调10%,当工作岗位为ds时,工资上调20%,其他正常,不修改数据库
select ename,job,
(case job when 'sd' then sal*1.1 when 'ds' then sal*1.2 else sal end)
as newsal 
from 
 emp;

4.分组函数(多行处理函数)

  • 需要先分组,才能用 多与group by 一同使用
  • 注意
    • 如果没有分组,则默认整张表为一组
    • 不需要对null 进行处理,自动忽略null
    • count(*) 与count(具体字段)的区别
      • count(具体字段)表示统计该字段(该列)所有不为null的元素的总和
      • count(*) 这里按行(因为不存在全为null的行)统计,即统计总行数量
    • __不能__在where条件中使用
    • 所有的分组函数可以联合使用
count 计数
sum 求和
avg 平均数
max 最大值
min 最小值
-- 实例
select min(asl) from emp
select max(asl) from emp
select sum(asl) from emp
select avg(asl) from emp
select count(ename) from emp

--  count(*) 
select count(*) from emp;
-- 表示一共有几行,即总行数量

select count(表头) from emp:
-- 即不为null的行的数量

5.分组查询

应用中需要先 进行分组,再对分组后的数据进行操作

select  ...  from  ... group by 表头
  • select 表头a, 分组函数(表头b) group by 表头a;

    在select 语句当中,如果有group by 语句,select 后只能跟:参加分组的字段,以及分组函数,

    其他一律不能跟(1.没有意义,2.若在oracle中报错,相较而言mysql 语法较为松散)

  • group by 表头1,表头2 两个字段联合分组

-- 应用
-- 找出每个部门,不同工作岗位的最高薪资
-- 技巧 两个字段联合分组
select deptno,jon,max(sal) from emp group by deptno,job
  • 使用having对分组后数据进行过滤

效率不高,实际上可以先where出再进行分组,where和having 优先使用having

select deptno,jon,max(sal) from emp group by deptno,job having sal>3000;
或者
select deptno,jon,max(sal) from emp where sal>3000 group by deptno,job ;
-- hvaing + 分组函数
select deptno,jon,max(sal) from emp group by deptno,job having avg(sal)>3000

补充

执行顺序

select ... from .. where .... group by ...order by..
先执行from ,再者 where ,接着是group by 之后是select 最后我order by..

从某张表中查询数据,
先经过where 条件筛选出有价值的数据,
对这些有价值的数据进行分组
分组之后可以使用having继续筛选
select查询出来
最后排序输出

distinct关键字

表示去除重复记录,原表数据不会改变,只是查询结果去重

  • distinct出现在一个字段前

    select distinct 表头 from emp 
    
  • distinct 前面不能出现字段

    select job,distinct  ename from emp; -- 报错
    
  • distinct 出现在所有字段前,表示联合去除重复

    select disctinct job,deptno from emp;
    
    • 可以使用count计算去除重复后的数量

      select count(distinct job) from emp;
      
原文地址:https://www.cnblogs.com/yescarf/p/14092434.html