数据库增删改查

新曾数据:insert into 表名 values(字段1值,字段2值,字段3值)insert into 表名(字段1,字段2,字段3)values(字段1值,字段2值,字段3值)
删除数据(不常用):delete from 表名 where 条件
修改数据:update 表名 set 字段1=字段值1,字段2=字段值2 where 条件 
查找数据:select * from 表名 where 条件
select 字段1,字段2 from 表名 where 条件

关键字查询(模糊查询)
查所有包含奥迪的汽车
select * from car where name like '%奥迪%'; #百分号%代表任意多个字符
查以'皇冠'开头的所有汽车
select * from car where name like '皇冠%';
查询汽车名称中第二个字符是'马'的
select * from car where name like '_马%'; #下划线_代表任意一个字符
排序查询
select * from car order by powers #默认升序排列
select * from car order by powers desc #升序asc 降序 desc
先按brand升序排,再按照price降序排
select * from car order by brand,price desc


范围查询
select * from car where price>40 and price<60
select * from car where price between 40 and 60

离散查询
select * from car where price=30 or price=40 or price=50 or price=60;
select * from car where price in(30,40,50,60)
select * from car where price not in(30,40,50,60)

聚合函数(统计查询)
select count(*) from car
select count(code) from car #取所有的数据条数
select sum(price) from car #求价格总和
select avg(price) from car #求价格的平均值
select max(price) from car #求最大值
select min(price) from car #求最小值

分页查询
select * from car limit 0,10 #分页查询,跳过几条数据(0)取几条(10)
规定一个每页显示的条数:m
当前页数:n
select * from car limit (n-1)*m,m

去重查询
select distinct brand from car

分组查询
查询汽车表中,每个系列下汽车的数量
select brand,count(*) from car group by brand
分组之后,只能查询该列或聚合函数

取该系列价格平均值大于40的系列代号
select brand from car group by brand having avg(price)>40

取该系列油耗最大值大于8的系列代号
select brand from car group by brand having max(oil)>8

 

posted @ 2017-04-09 08:46  梦深深处  阅读(180)  评论(0编辑  收藏  举报