sql语句1

sql基础

基本查询

  1. 查询某个字段
    select 字段名 from 表名

  2. 查多个字段
    select 字段名,字段名 from 表名

  3. 查询所有字 段
    select * from 表名

  4. 查询常量
    select 常量值
    注: 字符串和日期常量必须使用引号引起来,数值型不需要

  5. 查询函数
    select 函数名(实参列表)

  6. 查询表达式
    select 100/1024

  7. 起别名
    as 提高可读性
    空格

  8. 去重
    distinct
    select distinct 字段名 from 表名
    from distinct a,b from 表名 错误 a字段去重后和b字段去重后的总和不一定相等

  9. 加号(+)
    作用:做加法运算
    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

  10. concat函数 拼接字符
    select concat(字符1,字符2,字符3…)

  11. ifnull 函数
    判断某字段或者表达式是否为null,如果为null,返回指定的值,否则返回原本的值
    SELECT IFNULL(age,0) from table1
    在这里插入图片描述

  12. isnull
    判断某字段或者表达式是否为null,如果是null,返回1,否则返回0
    SELECT IsNULL(age) from table1
    在这里插入图片描述

  13. 条件查询
    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×
    >
  14. 排序查询
    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. 常见函数

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 求个数
    1. sum age 一般用于处理数值型

    2. max min count 可以处理任何类型

    3. 以上所有函数都忽略null值

    4. 和distinct搭配使用
      select sum(distinct salary),sum (salary) from employees;
      select count(distinct salary) from employees;

    5. count 函数
      select count(*) from employees;//统计总记录数
      select count(1) from employees;//统计总记录数
      select count(2) from employees;//统计总记录数

      效率:
      myisam存储引擎下,count()效率高
      innodb存储引擎下,count(
      )效率与count(1) 差不多,但都比count(‘字段’)高

    6. 和分组函数一同查询的字段有限制

分组查询 group by

select 分组函数,列(要求出现在分组函数的后面) from 表名 where 筛选条件 group by 分组列表 order by 子句

特点:

  1. 分组查询中筛选条件分为两类

    数据源位置关键字
    分组前筛选原始表group by 字句的前面where
    分组后筛选分组后的结果集group by 字句的后面having

    i:分组函数做条件肯定是放在having子句中
    ii:能用分组前筛选的,就优先考虑使用分组前的筛选

  2. group by 字句支持单个字段分组,支持多个字段分组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值