sql基础
基本查询
-
查询某个字段
select 字段名 from 表名 -
查多个字段
select 字段名,字段名 from 表名 -
查询所有字 段
select * from 表名 -
查询常量
select 常量值
注: 字符串和日期常量必须使用引号引起来,数值型不需要 -
查询函数
select 函数名(实参列表) -
查询表达式
select 100/1024 -
起别名
as 提高可读性
空格 -
去重
distinct
select distinct 字段名 from 表名
注 from distinct a,b from 表名 错误 a字段去重后和b字段去重后的总和不一定相等 -
加号(+)
作用:做加法运算
select 数值+数值;直接运算
select 字符+数值;试图将字符转换为数值,如果转换成功,则继续运算;否则转换为0,再做运算
select null+值;结果都为null
注
SELECT age+‘10’ as ages from table1
SELECT age+‘java’ as ages from table1
SELECT age+null as ages from table1 -
concat函数 拼接字符
select concat(字符1,字符2,字符3…) -
ifnull 函数
判断某字段或者表达式是否为null,如果为null,返回指定的值,否则返回原本的值
注 SELECT IFNULL(age,0) from table1

-
isnull
判断某字段或者表达式是否为null,如果是null,返回1,否则返回0
注 SELECT IsNULL(age) from table1

-
条件查询
13.1 语法
select 字段名 from 表名 where 筛选条件
13.2 筛选条件的分类- 简单条件运算符
< 、>、 =、 !=、 <=、>=、<=> 安全等于、<>不等于 - 逻辑运算符
and &&
|| or
! not - 模糊查询
like:一般搭配通配符使用,用于判断字符和数字
通配符:%任意多个字符,任意单个字符
between and
in
is null /is not null 用于判断null
注 SELECT age from table1 where age like '1_’

is null PK <=>
普通类型的值 null值 可读性 null × √ > √ √ - 简单条件运算符
-
排序查询
select 字段列表 from 表名 where 筛选条件 order by 排序列表 【asc、desc】
注
14.1按照别名SELECT age*(1+IFNULL(age,0)) as ages from table1 order by ages 别名;
14.2 按照表达式SELECT age*(1+IFNULL(age,0)) as ages from table1 order by age*(1+IFNULL(age,0));
14.3按照函数SELECT LENGTH(user_name) as length from table1 order by LENGTH(user_name);
14.4 按照多个字段排序SELECT user_name,age from table1 order by user_name desc, age asc;

-
常见函数
15.1 单行函数
concat 、length 、ifnull
字符函数
- length 获取参数值的字节
select length(‘join’) 4
select length(‘张三丰hahaha’) 15 - concat 拼接字符串
select concat(aa,bb,"-",cc) - upper lower
select upper(‘join’) - substr substring
注 索引从1开始
select substr(‘李莫愁爱上陆展元’,6) // 陆展元
注 截取从1开始,长度为3个字符
select substr(‘李莫愁爱上陆展元’,1,3) // 李莫愁 - instr 第一次子串在主串中出现的索引位置,如果没有,返回0
select instr(‘杨不悔爱上殷六侠’,‘殷六侠’) as output //6 - trim 去掉前后的空格
select trim(’ 张三 ')
select trim(‘a’ from ‘aaa张aaaa三aaaaa’) //张aaaa三 - lpad 用指定的字符左填充指定长度
select lpad(‘殷素素’,10,’*’) as output //*******殷素素
-rpad 用指定的字符左填充指定长度
select rpad(‘殷素素’,10,‘ab’) as output //殷素素ababababa
-replace 替换
select replace(‘张无忌爱上周芷若’,‘周芷若’,‘赵敏’)//张无忌爱上赵敏
数学函数
- round 四舍五入
select round(-1.45) // -1
select round(-1.65) //-2
select round(1.234,2)//1.23 - ceil 向上取整,返回大于等于该参数的最小整数
select ceil(1.00) //1
select ceil(-1.02)// -1 - floor 乡下取整,返回小于等于该参数的最大整数
select floor(1.00) //1
select floor(-1.02)// -2 - truncate 截断
select truncate(1.65,1) // 1.6 - mod 取余
select mod(10,3) //1
select mod(-10,-3) //-1
select mod(10,-3) //1
日期函数
- now() 返回当前系统日期
select NOW(); - curdate() 当前系统时间,不包括时分秒
- curtime() 当前系统时间,不包括年月日
- 可以获取指定的部分年 月 日 时 分 秒
select YEAR(NOW()) // 2021
select MONTH(NOW()) //2
select MONTHNAME(NOW()) //February - str_to_date 将指定的字符串转换为日期
select str_to_date(‘1988-3-4’,’%Y-%c-%d’) //1988-03-04 - date_format 将日期转换为字符
select date_format(NOW(),’%y年%m月%d日’) 21年2月28日
其他函数
- 查看版本
select version(); - 查看数据库
select database(); - 查看用户
select user();
流程控制函数
- if if else
select if(10>5,‘big’,‘small’) // big - case 相当于switch case
case 要判读的表达式
when 常量1 then 要显示的值;
when 常量2 then 要显示的值;
else 要显式的值;
end
select salary
case
when salary>20000 then ‘A’
when salary>20000 then ‘B’
when salary>20000 then ‘C’
else ‘D’
end as ‘工资级别’
from emlpoyees;
15.2分组函数(统计或者聚合函数)
- sum 求和
- avg 求平均值
- max 求最大值
- min 求最小值
- count 求个数
- 注
-
sum age 一般用于处理数值型
-
max min count 可以处理任何类型
-
以上所有函数都忽略null值
-
和distinct搭配使用
select sum(distinct salary),sum (salary) from employees;
select count(distinct salary) from employees; -
count 函数
select count(*) from employees;//统计总记录数
select count(1) from employees;//统计总记录数
select count(2) from employees;//统计总记录数效率:
myisam存储引擎下,count()效率高
innodb存储引擎下,count()效率与count(1) 差不多,但都比count(‘字段’)高 -
和分组函数一同查询的字段有限制
-
分组查询 group by
select 分组函数,列(要求出现在分组函数的后面) from 表名 where 筛选条件 group by 分组列表 order by 子句
特点:
-
分组查询中筛选条件分为两类
数据源 位置 关键字 分组前筛选 原始表 group by 字句的前面 where 分组后筛选 分组后的结果集 group by 字句的后面 having i:分组函数做条件肯定是放在having子句中
ii:能用分组前筛选的,就优先考虑使用分组前的筛选 -
group by 字句支持单个字段分组,支持多个字段分组
323

被折叠的 条评论
为什么被折叠?



