Mysql 基础3

 

1. 逗号是个好东西
2.对于多条件查询 和范围查询 的灵活运用(and 和or 的灵活运用)
in 用的时候注意 补充

select * from car where name like '%奥迪%' and price between 20 and 70 order by Powers,oil desc


相同类型条件之间 用and (比如两个where) 其他用空格 
排序时 注意 逗号前面的 表示先排序 如有重复的 再按都好后面的排序
: 例子表示 查找所有 名字中有奥德的车 选取 20--70 的 按照 功率排序如有重复 重复的部分按照 油耗排序

查询汽车表中每个系列下有多少个汽车
select brand,count(*) from car group by brand ;#输出品牌 + 数量 {后面的brand 表示品牌形同的归为一组; 前面 brand 表示 输出品牌; 同样 如果 后面 brand 换位 price 表示 输出品牌 价格相同的 归为一组}
 
 
where 表示 行 例如 where name=“mm” 表示: mm所在的一行
 
 
 
条件 为 true 即可查出结果 例如条件为: {a=a} 或者直接为 整数 或true
如果为 false 则 查询结果为空
select * from info where price between 40 and 70;
select * from info where true;
select * from info where 1;

 

 

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

查询     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 distinct 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;

转载于:https://www.cnblogs.com/ordinaryk/p/6124974.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值