查询语句:select * from 表名
模糊查询:like ‘%’
select * from 表名 where like ‘需要查询的字符%’
添加语句:insert into 表名(字段) values (与表中字段个数一致或与前面字段一致)
修改语句:update 表名 set 字段 where 条件
删除语句:delete from 表名 where 条件
聚合函数:sum()(数值求和)、avg()(数值求平均数)、count()(求总行数)、max() (求字段中的最大值)、min()(求字段中的最小值)
排序:desc、asc
select * from 表名 order by 数值字段 desc/asc
Limit关键字:limit 开始行, 需要查询的行数 (一般用在order by后)
分组: group by
select 分组字段,聚合函数 from 表名 group by 字段
例: 查询班级中的人数
select class, count(*) from 表名 group by class
having;后加条件。(该关键字用于group by后)
多表查询:
基础连接:
select * from 表1,表2 where 表1.字段 = 表2.字段
内外连接:
内连接: inner join
select * from 表1 inner into 表2 on 表1=表2
外连接:left join(左外连接), right join(右外连接)
左外连接:select * from 表1 left join 表2 on 表1=表2
右外连接:select * from 表1 right join 表2 on 表1=表2
排重查询:distinct
select distinct 字段 from 表名
合并数据语句:union、union all
select 字段 from 表名 union (all) select 字段 from 表名
时间函数:date(时间字段)(给字段添加当前时间)、
datediff(时间1、时间2)(时间1-时间2的差)、
now()(当前时间)
子查询:select * from 表1 where 表1.字段 = (select 字段 from 表2)
子查询的语句只要查询出一个数据