CRUD简单查询

一、查询所有数据

select * from car

二、查询指定列

select code , price from car

 

三、修改查询出的列名

select code as '代号' , name as '车牌' from car

 

四、条件查询

单条件查询

select * from car where code='c002'

多条件查询(用or或者and链接)

select * from car where code='c002'or price='37.16'

select * from car where code='c002' and price='67.49'

五、模糊查询(关键字查询)

%关键字,关键字前面有n个字符;关键字%,关键字后面有n个字符

_关键字,关键字前面有一个字符;关键字_,关键字后面有一个字符

select * from car where name like '奥迪%'

六、排序查询

desc降序  asc升序。 

默认为升序

select * from car order by price desc

 根据价格降序排列

select *from car order by price desc, oil desc

 先根据price降序排列,有重复的 在根据oil降序排列

七、去重查询

distinct去重
select distinct brand from car

 

八、分页查询

select * from car limit 5, 5     #跳5条不包括5,取5条

九、统计查询(聚合查询)

数据条数

select count(*) from car
select count(code) from car

 

select max(列名) from 表名 #取最大值
select min(列名) from 表名#取最小值
select avg (列名)from 表名#取平均值

 十、分组查询

select * from car group by  brand
select brand ,count(*)from car group by brand

 

select brand from car group by brand having count(*)>=3 #有having前面必须有group by
有group by 后面可以没有having

 

十一、范围查询  in 和 not in   相反 一个是是这些数的  一个是不是这些数的

select * from car where price between 40 and 60 

十二、离散查询

select * from car where price in (10,20,30,40,50,60)

select * from car where price not in (10,20,30,40,50,60)

原文地址:https://www.cnblogs.com/navyouth/p/8143791.html