码迷,mamicode.com
首页 > 数据库 > 详细

Mysql 基础3

时间:2016-12-02 11:44:04      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:范围查询   平均值   sel   between   大于   聚合   mil   car   排列   

 

查询     select.... from...

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

select*from表名     注:  *  查所有的列

--------------------------------------------------------------------------------------

二.查询指定列的数据   (查询结果是虚拟的)

select 列名,列名from 表名

例子:select code,name from info;

三.修改结果集的列名

select code as ‘代号’,name as ‘姓名’  from info

----------------------------查询行-----------------------------------------------

四.出啊讯指定行的数据

select *form info where code=’p003’;

.多条件查询

查询  info表中  codep003 或者 nation=n001’的

select *form info where code=’p003’ or  nation=’n001’;

查询  info表中  codep003 并且 nation=n001’的

select *form info where code=’p003’  and  nation=’n001’;

六. 范围查询

select * from  info where price>=40 and price<=70;

select * from  info where  price between 40 and 70;

 

 

------------------------------------------------------------------------in

. 离散查询

查询汽车价格(20,32,423,54,657,787)内的所有车

select *from info where price in(20,32,423,54,657,787);

查询汽车价格不在(20,32,423,54,657,787)内的所有车

select *from info where price  not  in(20,32,3,54,657,787);

 

-----------------------------------------------------------------------like

八. 模糊查询

查询表里的名称还有 奥迪的

select*from car where name like ‘%奥迪%’    % 表示任意n 个字符

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

select * from car where name like’_’         _表示一个字符

.排序查询

价格升序排列

select*from car order by price asc           asc升序 (可以省略)

价格降序排

select*from car order by price desc           asc升序 (可以省略)

先按  brand 排列  再按  price 排列

select*from car order by  brandpricedesc;

十. 去重查询

 select distance brand from car;

十一;

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

select*from car limit 20,10

---------------------------------------------------------------------------------------------------------------------

十二. 聚合函数 (统计函数)

  select count(*) from  chinastates  #查询数据总条数

 select count(areacode) from  chinastates  #查询数据总条数 括号呢  变成主键列  提高运行效率

 select count(areacode) from  chinastates  #查询数据总条数 括号呢  变成主键列  提高运行效率

 select sum(price) from car    求和

 select ave(price) from car    平均值

 select max(price) from car    最大

 select min(price) from car    最小

---------------------------------group by.......having-------------------------------------------------------------

十三. 分组查询

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

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

查询车店  卖的汽车 数量大于4

select  brand from car group by brand  having  count(*)>3;

Mysql 基础3

标签:范围查询   平均值   sel   between   大于   聚合   mil   car   排列   

原文地址:http://www.cnblogs.com/ordinaryk/p/6124974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!