数据库复习

查询:

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 表名;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值