当前位置:Gxlcms > 数据库问题 > 数据库部分---查询-简单查询;

数据库部分---查询-简单查询;

时间:2021-07-01 10:21:17 帮助过:51人阅读

1.最简单查询(查询所有数据)

select * from 表名;*代表所有的列

技术分享技术分享

 


 

2.查询指定列的数据

select 列名1,列名2 from 表名

技术分享技术分享


 

3.修改结果集的列名

select 列名1 as ‘代号1‘,列名2 as ‘代号2‘ from 表名

技术分享技术分享

 


 

4.条件查询

select * from 表名 where 列名=‘’;

技术分享技术分享

 

 


 

5.多条件查询

select * from 表名 where 列名1=‘’ or 列名2=‘‘;

技术分享技术分享

 

 

select * from 表名 where 列名1=‘’ and 列名2=‘‘;

 


 

6.范围查询

以汽车表价格为例,选出价格在40到60之间的:

select * from 表名 where 列名>=40 and 列名<=60;

select * from 表名where 列名 between 40 and 60;

技术分享技术分享

 

 


 

7.离散查询

查询汽车价格在(10,20,30,40,50,60)中出现的汽车信息

select * from car where price=10 or price=20 or price=30 or price=40 or price=50 or price=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);

技术分享

 


 

8.模糊查询(关键字查询)

查询car表里面名称包含奥迪的

select * from car where name like ‘%奥迪%‘;            %代表任意几个字符

技术分享

 

select * from car where name like ‘%奥迪‘;            以‘奥迪’结尾的

select * from car where name like ‘奥迪%‘;           以‘奥迪’开头的

查询car表中名称第二个字符为‘马’的汽车

select * from car where name like ‘_马%‘;            _代表任意一个字符;


 

9.排序查询

汽车表

select * from car order by price asc   ;                  按照价格升序排序。     asc代表升序(因为默认升序,可以省略)

select * from car order by oil desc  ;                  按照油耗降序排列。   desc代表降序。

先按照brand升序排,再按照price降序排

select * from car order by brand asc,price desc;   当brand里边数据重复时,再按照price排列

 技术分享

 

 


 

10.去重复查询

select distinct 列名from 表名;

select distinct brand from car;

技术分享

 


 

11.分页查询

一页显示10条 当前是第2页

select * from car limit 10,10;      第一个10代表跳过10条,第二个10代表显示10条

技术分享技术分享

 

一页显示m条 当前是第n页

limit (n-1)*m,m;


 

12.聚合函数(统计函数)

select count(*) from chinastates    查询chinastates表中的数据总数

select count(主键列名) from chinastates    查询chinastates表中的数据总数

 

select sum(price) from car;   sum代表求和,求汽车表中,所有汽车的总价格

select avg(price) from car;  avg代表求平均值。求汽车表中,所有汽车价格的平均值

select max(price) from car;求汽车表中,所有汽车价格中的最大值

select max(price) from car;求汽车表中,所有汽车价格中的最小值


 

13.分组查询

查询汽车表中,每个系列下有多少个汽车

select brand,count(*) from car group by brand ;         from car group by brand --按照brand分组。select brand,count(*)--并查询brand和每组的数量

技术分享

 

 

查询汽车表中卖的汽车数量大于3的系列

select brand from car group by brand having count(*)>1;       from car group by brand  --按照brand分组。       having+条件--having count(*)>1;数量大于1的

技术分享

 

数据库部分---查询-简单查询;

标签:函数   均值   多条件   des   where   信息   order by   from   order   

人气教程排行