MySQL语法整理

383 阅读6分钟

进阶一、基础查询

语法:
    select 查询列表 from 表名

特点

  • 1.查询列表可以是:表中的字段、常量、常量值、表达式、函数
  • 2.查询的结果是一个虚拟的表格
USE myemployees;

1.查询表中的某个字段

select last_name from employees; 

2.查询表中的多个字段

select last_name,salary,email from employees;

3.查询表中的所有字段

select * from employees;

4.查询常量值

select 100;
select 'john';

5.查询表达式

select 100*98

6.查询函数

select version();

7.起别名

  • 1.便于理解
  • 2.如果要查询的字段有重名的情况,使用别名可以区分开来
方式一: 使用as
select 100%98 as 结果; 
select last_name as 姓, first_name as 名 from employees;

方式二: 使用空格
select last_name 姓, first_name 名 from employees;

案例:查询salary,显示结果为out put,若别名中有特殊符号,别名需加双引号
select salary as "out put" from employees;

8.去重

案例: 查询员工表中涉及到的所有部门编号
select distinct department_id from employees;

9.+号的作用:运算符

1、两个操作数都为数值型,则做加法运算
select 100+90;

2、其中一方为字符型,试图将字符型数值转换为数值型,如果转换成功,则继续做加法运算
select '123' + 90;

3、如果转换失败,则将字符型数值转换为0
select 'john' + 90;

4、只要其中一方为null,则结果肯定为null
select null + 90;

进阶二、条件查询

语法:
    select 查询列表 from  表名 where 筛选条件;

分类

  • 按条件表达式筛选

      简单条件运算符: > < = != <> >= <=
    
  • 按逻辑表达式筛选

      逻辑运算符
      作用: 用于连接条件表达式
          &&  ||  !
          and or  not
      && 和 and:两个条件都为true,结果为true,反之为false
      || 和 or: 只要有一个条件为true,结果为true,反之为false
      ! 和 not:如果连接的条件本身为false,结果为true,反之为false
    
  • 模糊查询

      like
      between and
      in
      is null
    

1.按条件表达式查询

案例1: 查询工资>12000的员工信息

select * from employees where salary > 12000;

案例2: 查询部门编号不等于90号的员工名和部门编号

select last_name, department_id from employees where department_id <> 90;

2.按逻辑表达式筛选

案例1:查询工资在10000到20000之间的员工名、工资以及奖金

select last_name,salary,commission_pct from employees where salary >= 10000 and salary <= 20000;

案例2:查询部门编号不是在90到110之间,或者工资高于15000的员工信息

select * from employees where department_id < 90 or department_id < 110 or salary > 15000;

3.模糊查询

3.1 like

  • 特点:一般和通配符一起使用
  • 通配符: % 任意多个字符,包含0个字符; _ 任意单个字符

案例一:查询员工名中包含字符a的员工信息。%代表通配字符,代表任意一个字符

select * from employees where last_name like '%a%';

案例二:查询员工名中第三个字符为e,第五个字符为a的员工名和工资

select last_name, salary from employees where last_name like '__e_a%';

案例三:查询员工名中第二个字符为_的员工名

select last_name from employees where last_name like '_$_%' escape '$';  #escape 后代表转义字符  

3.2 between and

  • 使用between and可以提高语句的简洁度
  • 包含临界值
  • 两个临界值不要调换顺序

案例一:查询员工编号在100到200之间的员工信息

select * from employees where employee_id>= 100 and employee_id <= 200;

----------------------

selct * from employees where employee_id between 100 and 200;

3.3 in

  • 含义: 判断某字段的值是否属于in列表中的某一项
  • 特点:
    • 使用in提高语句简洁度
    • in列表的值类型必须一致或兼容
    • in列表中不支持通配符

案例:查询员工的工种编号是IT_PROG、AD_VP、AD_PRES中的一个员工名和工种编号

select last_name,job_id from employees where job_id = 'IT_PROG' or job_id = 'AD_VP' or job_id = 'AD_PRES';

--------------------

select lase_name, job_id from employees where job_id in ('IT_PROG', 'AD_VP', 'AD_PRES');

3.4 is null

  • = 或 <> 不能用于判断null值
  • is null 或 is not null 可以判断null值

案例1:查询没有奖金的员工名和奖金率

select last_name,commission_pct from employees commission_pct is null;

案例二:查询有奖金的员工名和奖金率

select last_name,commission_pct from employees commission_pct is not null;

案例三:查询员工的工资为12000的员工名和奖金率

select last_name, commission_pct from employees where salary is 12000;

3.5 安全等于 <=>

案例:查询没有奖金的员工名和奖金率

select last_name, commission_pct from employees where commission_pct <=> null;

案例:查询工资为12000的员工信息

select * from employees where salary <=> 12000;
  • is null VS <=>
    • is null:仅仅可以判断null值,可读性较高,建议使用
    • <=>:既可以判断null值,又可以判断普通的数值,可读性较低

进阶三、排序查询

  • 语法:
    select 查询列表
    from 表名
    【where 筛选条件】
    order by 排序的字段或表达式
    
  • 特点:
    • asc 代表的是升序,默认是升序,可以省略;desc 代表的是降序
    • order by 子句可以支持单个字段、别名、表达式、函数、多个字段
    • order by 子句一般是放在查询语句的最后面,除了 limit 语句

1.按单个字段查询

select * from employees order by salary desc;

2.添加筛选条件再排序

案例:查询部门编号>=90的员工信息,并按员工编号降序

select * from employees where department_id >= 90 order by employee_id desc;

3.按条件表达式排序

案例:查询员工信息,按年薪降序

select *,salary*12*(1+IFNULL(commission_pct,0)) from employees order by salary*12*(1+IFNULL(commission_pct,0)) desc;

4.按别名排序

案例:查询员工信息 按年薪升序

select *,salary*12*(1+IFNULL(commission_pct,0)) 年薪 from employees order by 年薪 asc;

5.按函数排序

案例:查询员工名,并且按名字的长度降序

select LENGTH(last_name),last_name from employees order by LENGTH(last_name) desc;

6.按多个字段排序

案例:查询员工信息,要求先按工资降序,再按employee_id升序

select * from employees order by salary desc,employee_id asc;

进阶四、常见函数

  • 概念:类似于java的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
  • 好处:
      1. 隐藏了实现细节
      1. 提高代码的重用性
  • 调用:select 函数名(实参列表) 【from 表】
  • 特点:
    • 1.叫什么(函数名)
    • 2.干什么(函数功能)
  • 分类:
    • 1.单行函数
    • 2.分组函数

1.字符函数

1.1 length 获取参数值的字节个数

select length('john');

1.2 concat 拼接字符串

select concat(last_name,'_',first_name) 姓名 from employees;

1.3 upper lower 变大写 变小写

select upper('john');
select lower('joHn');
#将姓变大写,名变小写,然后拼接
select concat(upper(lase_name),lower(first_name)) 姓名 from employees;

1.4 substr/substring 截取字符;注意:索引从1开始

##截取从指定索引处后面所有字符
select substr('李莫愁爱上了陆展元',7) output;

##截取从指定索引处指定字符长度的字符
select substr('李莫愁爱上了陆展元',1,3) output;

1.5 instr 返回子串第一次出现的索引,如果找不到返回0

select instr('杨不悔爱上了殷六侠','殷八侠') as output;

1.6 trim 去除首尾

select trim('   abc       ') as output; ##abc
select trim('a' from 'aaaaaabaaaaaacdaaaaaaa') as output; ##baaaaaacd

1.7 lpad 用指定的字符去实现左填充指定长度

select lpad('殷素素',2,'*') as output; ##殷素
select lpad('殷素素',5,'*') as output; ##**殷素素

1.8 rpad 用指定的字符去实现右填充长度

select rpad('殷素素',12,'ab') as output; ## 殷素素ababababa

1.9 replace 替换

select replace('周芷若周芷若周芷若周芷若张无忌爱上了周芷若','周芷若','赵敏') as out_put; #赵敏赵敏赵敏赵敏张无忌爱上了赵敏

未完待续