基础查询
1、新增
insert into 表名(key1,key2) values(val1,val2) 或者
insert into 表名(val1,val2)
2、更新
update 表名 set key1=val1,key2=val2 where 条件
3、删除
delete from 表名 where 条件
4、查询
select * from 表名 where 条件 # 筛选出符合条件的所有字段信息
select 字段名 from 表名 where 条件 # 筛选符合条件的指定字段名
select 字段名 as “别名” from 表名 where 条件 # 筛选出制定字段信息,字段显示别名
5、去重 distinct
select distinct 字段名 from 表名 where 条件 # 筛选符合条件的数据,根据筛选字段值去重
6、条件查询
#比较运算符
>、<、 =、 <>、 <=、 >=、 between ... and ..、 in(a,b,c)、 like (一个占位符“_”,匹配任意字符 “%”) 、is null、
#逻辑运算符
or / || 或、and / && 且、not / ! 非,不是
7、聚合函数
count 统计数量、max 最大值、min 最小值、avg平均值、sum求和
8、分组查询 group by having
select 字段 from 表 where 条件 group by 分组字段名 having 条件
eg:select count(*) from user group by sex 统计男女各多少人
9、排序order by
select 字段 from 表 order by age asc 生序 desc倒序
select * from user orderby age desc , timeday desc #先根据年龄倒序排列,年龄相同在根据入职时间倒序排列,多次排序使用都好分隔开
10、分页查询 limit
select * from 表 limit 索引数(页码-1* 每页展示数) 数量
select * from 表 user limit 0,10 查看第一页10条数据
select * from 表 user limit 10,10 查看第二页10条数据
select * from 表 user order by age limit 0,3 查看年龄最大的前三个人
11、语句执行顺序
from > where > group by > having > select > order by > limit
12、练习题
#查询年龄为20,21,22,23岁的女性员工信息
select * from user where gender = "女" and age in(21,22,23)
#查询性别为男,并且年龄在20-40岁的且姓名为三个字的员工
select * from user where gender= "男" and age between 20 and 40 and name like "____"(三个下划线,匹配任意字符)
#统计员工表,年龄小于60岁,男性员工和女性员工的人数
select gender count(*) from user where age<60 group by gender
#查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按照入职时间降序排序
select name,age from user where age<=35 order by age asc,entrydate desc
#查询性别为男,且年龄在20-40岁以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同按入职时间升序排序
select * from user where gender = "男" and age between 20 and 40 order by age asc,entrydate asc limit 5