普通查询
1.DESC表名 ;/*显示表结构*/
2.Select distinct列明 from 表明;/*distinct 去重*/
3.Select concat(列名1,列名2) as 别名 from 表明 ;/*concat 连接,as 别名*/
4.select ifnull(可能为null的列名,0)from 表名 ;/*ifnull判断为null,用0替换*/
条件查询where
[if !supportLists]1. [endif]select列名 from 表明 where 条件表达式
[if !supportLists]2. [endif]条件表达式符号&& /||/ !/ and/ or /not()
模糊查询
Like /between/ and/ in/ is null/ is not null
1.select * from表名 where 列名 like ‘模糊查询字符’;比如’__a__c%’代表第三个字符为a第6个字符为c。(通配符%包含多个字符 _任意一个字符,\转义:\_表示下划线,转义字符也可以自定义,例如like‘_$_%’ escape ‘$’)
2. select * from表名 where 列名 in(条件);
排序查询
Select * from表名 where 查询条件 order by 列名 DECS/ASC
字符函数
[if !supportLists]1. [endif]substr(列名,1(位置),1(长度))
[if !supportLists]2. [endif]In(“字符”,“字符”)
数学函数
[if !supportLists]1. [endif]四舍五入
Select round(-1.55,1)
[if !supportLists]2. [endif]向上、下取整
Select ceil/floor()
[if !supportLists]3. [endif]截断truncate()
[if !supportLists]4. [endif]取余mod(10,-3)=10%3
If函数
if(10>8,true,false)
Case
Select salary,department_id,
Case department_id
When 30 then salary*1.1
When 40 then salary*1.3
Else/*默认值或者别的*/ salary
End as新工资
From表名
If-else
Select salary
Case
When salary>2000 then “A”
When salary>2000 then “B”
Else “C”
End as工资级别
from表名
分组函数
Sum /min/max/count/avg
Select sum(distinct列名) from表名
Datediff(“2019-09-09”,“2019-09-19”)/*日期之差*/
分组查询
Select分组函数,列(要求出现在group by的后面)
From表
Where筛选条件
Group by分组的列表
Order by ...
例子:查询每个工种(job_id)的最高工资
Select max(salary),job_id
From表
Group by job_id
例子:1.查询每个部门的员工个数
2.根据一的查询结果进行筛选,查询哪个部门的员工个数>2
Select count(*),department_id
From employee
Group by department_id
Havingcount(*)>2(分组后筛选)
Order by...
多表查询
92语法
Select name,boyname(列名) from boys b,beauty e(表名)
Where b.boyfriend_id=e.id
And。。。
99语法
Select列名
From表一 【连接类型】
Join表2
On连接条件=等值连接/between and非等值连接/
Where筛选条件
Group by
Having
Order by
内连接:inner
外连接:left【outer】主表,从表
Right【outer】从表,主表
Full【outer】
交叉连接:cross
子查询
子查询出现的位置:
Select后面:仅仅支持标量子查询
From后面:支持表子查询
Where或者having后面:标量子查询、列子查询,行子查询。
Exists:表子查询
例子:谁的工资比Abel高
[if !supportLists]1. [endif]查询abel的工资
Select salary from employee where last_name=”abel”
[if !supportLists]2. [endif]查询所有人的信息
Select salary
from employee
where salary>/in/all/any(
Select *
from employee
where last_name=”abel”
)
多行子查询操作符
In/not in等于列表中的任意一个
Any/some和子查询返回的某一个值比较
All和子查询返回的所有值作比较
分页查询
Select列名
From表一 【连接类型】
Join表2
On连接条件=等值连接/between and非等值连接/
Where筛选条件
Group by
Having
Order by
Limit offset(要显示的条目的起始索引),size(要显示的条目个数);
查询前五个员工信息
Select * from employee
Limit 0,5;
插入
Insert into表名(列名。。。)values(),(),();
Insert into表名 set 列名=值,列名=值;
修改
Update表名
Set列=值
Where筛选条件
Update可多表更新
例子:修改张无忌女朋友的手机号
Update doyb
Inner join beauty b on bo.id=b.boyfriend
Set b.phone=””
Where bo.boyname=”张无忌”
删除
单表:Delete from表名 where筛选条件
多表: delete表一的别名,表二的别名
From表一 别名
Inner join表二别名
on连接条件
where筛选条件
Truncate table表名/*删除表里所有数据*/