数据库的简单查询

1.增加内容

insert into xuesheng values('5','赵六','1','16')
# insert into<表名>values(<'值1'>,<'值2'>,<'值3'>,<'值4'>)

insert into xuesheng (xuehao,xingming) values('6','朱八')
# insert into <表名>(<列1>,<列2>)values(<'值1'>,<'值2'>)

2.删除数据

delete from xuesheng where xuehao='8'
# delete from <表名> where <列1>=<'值1'>

3.修改数据

update xuesheng set xingming='田七' where xuehao='6'
# update <表名> set <列1>=<'值1'> where <列1>=<'值1'>

4.查询数据

(1)简单查询

select * from xuesheng
# select * from <表名>

select xuehao as'学号',xingming as'姓名' from xuesheng
# select <列1> as <'中文'>,<列2>as<'中文'> from <表名>

(2)条件查询

select * from car where code='c002'
# select * from <表名> where <列1>=<'值1'>

select * from car where brand='b001' and powers=130
# select * from <表名> where <列1>=<'值1'> and <列2>=<'值2'>

select * from car where brand='b001' or powers=130
# select * from <表名> where <列1>=<'值1'> or <列2>=<'值2'>

(3)模糊查询

%代表任意多个字符 _代表一个字符

select * from car where name like '奥迪%'
# select * from <表名> where <列1> like <'值1%'>

select * from car where name like '%型'
# select * from <表名> where <列1> like <'%型'>

select * from car where name like '%舒适%'
# select * from <表名> where <列1> like <'%舒适%'>

select * from car where name like '__A6L%'
# select * from <表名> where <列1> like <'__A6L%'>

(4)排序查询

select * from car order by powers
# select * from <表名> order by <列名> 默认升序

select * from car order by powers desc
# select * from <表名> order by <列名> desc 降序

select * from Car order by Brand,Powers desc
# select * from <表名> order by <列1><列2> desc 多个列名查询

(5)范围查询

select * from car where price>=40 and price<=60
# select * from <表名> where <列名>>=<值1> and <列名><=<值2>

select * from car where price between 40 and 50
# select * from <表名> where <列名> between <值1> and <值2>

(6)离散查询

select * from car where code in ('c001','c005','c008')
# select * from <表名> where <列名> in (<'值1'>,<'值2'>,<'值3'>)

select * from car where code not in ('c001','c005','c008')
# select * from <表名> where <列名> not in (<'值1'>,<'值2'>,<'值3'>)

(7)聚合函数:统计查询

select sum(price) from car
# select sum(<列名>)from <表名>
# 查询所有价格之和 sum() 求和

select count(price) from car
# select count(<列名>) from <表名>
# 查询数据条数

select max(code) from car
# select max(<列名>) from <表名>
# 求最大值

select min(code) from car
# select min(<列名>) from <表名>
# 求最小值

select avg(price) from car
# select avg(<列名>) from <表名>
# 求平均值

(8)分页查询

select * from Car limit 10,5
# select * from <表名> limit (n-1)*5,5
# 每页显示5条数据,取第2页的数据

(9)去重查询

select distinct brand from car
# select distinct <列名> from <表名>

(10)分组查询
select count(*),brand from car group by brand
# select count(<所有全部>),<列名> from <表名> group by <列名>

select brand from car group by brand having count(*)>1
# select <列名> from <表名> group by <列名> having count(<所有全部>)>1
# 分组之后根据条件查询使用having 不使用where

原文地址:https://www.cnblogs.com/chenchen0815/p/5530734.html