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

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的

 14.取一段时间内的所有数据,时间可以在语句中直接进行比较

$sql="select sum(sprice) from ruzhu where ruzhutime between '2017-02-09 08:00:00' and '2017-02-09 20:00:00'";
原文地址:https://www.cnblogs.com/xingyue1988/p/6124860.html