查询:
select 字段名 from 表名; #查询指定表字段
select 字段名1,字段名2 from 表名;
select * from 表名; #查询表中所有表字段
条件:
select 字段名 from 表名 where 条件;
select 字段名 from 表名 where 字段名 is null;
select 字段名 from 表名 where 字段名 is not null;
select 字段名 from 表名 where 条件1 and/or 条件2; #和;
#比较运算 > < >= <= != <> 最后两个都可看做不等于
select 字段名 from 表名 where 条件1 between 条件2 #在两者之间
select 字段名 from 表名 where 字段名 in(value1,value2...);#与or的用法相同
select distinct 字段名 from 表名; #distinct:去重复
主键约束:primary key 自增1:auto_increment
create table 表名(字段名 类型 primary key auto_increment);
模糊查询like
%: 代表0或多个字符
_: 代表一个未知字符
select * from 表名 where name like "孙%" #姓名以孙开始的信息查询
select * from 表名 where name like "%空" #姓名以空结尾的信息查询
select * from 表名 where nmae like "%孙%" #姓名中包含孙的信息查询
select * from 表名 where name like "_悟" #姓名中第二个字是悟字的信息查询
排序order by
select * from 表名 order by 字段名; #通过某个字段排序,asc(默认升序)/desc(降序)
#若想排序字符串数据 可以用lpad方法左补齐 (字段名,向左补齐的位数,用什么元素补齐)
select * from 表名 order by lpad(name,4,0);
分页查询 limit
#格式: limit 跳过的条数,请求的条数(每页的条数)
#跳过的条数=(请求的页数-1)*请求的条数(每页条数)
#列如: 查询一个表中的第三页,的五条数据;
select * from 表名 order by 字段名 limit 10,5;
别名
select 字段名 as "别名" from emp;
select 字段名 "姓名" from emp;
select 字段名 姓名 from emp;
聚合函数
select avg(字段名) from 表名;#取平均值
select sum(字段名) from 表名;#求和
select max(字段名) from 表名;#求最大值
select min(字段名) from 表名;#求最小值
select count(*) from emp;#计数
数值计算
select 字段名*5 from 表名;
select 字段名=字段名+5 from 表名;