Mysql(加强) 之查询语句select 上

1.查询表的多有行所有列:

        select * from user;

2.查询一行:

        select * from user where uid=2;

 3.查询多行:

         select * from user where uid>=2;

 4.查询某几行的某列,*代表所有列:

         select uid,name from user where uid>=2;

         select name from user where uid=2;

 4.1.列是变量

  每一行,变量值在变

  从上到下,遍历行,where 是表达式,当 值为真,则取出该行

  select * from user where 1; //取出所有行

  select * from user where 0; //一行都取不出来

  4.2.变量可以计算

  计算明年多少岁

  select name,age+1 from user where 1;

  4.3.floor 向下取整

   select floor(num/10)*10 from mian; 

   4.4.where 条件查询中常见的限制条件

   <   小于     

   != 或 <>   不等于

   <=   小于或等于

   >=  大于或等于

   =    等于(这里是一个等号,不同于编程语言的两个等号)

   >    大于 

   in   在某个集合内

   between  在某个范围内

   NOT  或  !       逻辑非

   OR  或  ||         逻辑或

   AND 或  &&     逻辑与

5.group 分组与统计函数:

统计函数

count() 计算行数

avg() 求平均函数

sum() 求总和

min() 求最小

max() 求最大

5.1.查询所有商品的平均价格

select avg(shop_price) from goods;

5.2.查询最贵的商品

select max(shop_price) from goods;

5.3.查询最便宜的商品

select min(shop_price) from goods;

5.4.查询一共有多少种商品

select count(*) from goods;

5.5.查询商品的总价格

select sum(shop_price) from goods;

5.6.查询本店积压的商品总贷款

select sum(shop_price*goods_number) from goods;

5.7.group by 统计每个栏目下商品的平均价格

select cat_id,avg(shop_price) from goods group by cat_id;

5.8.查询每个栏目下有多少种商品

select cat_id,count(*) from goods group by cat_id;

6. having 筛选 

having 与 where 的区别 :

where查询针对的是磁盘,“select sname,(age-sno) as a from student where a > 10;”

where之前的select查询结果也就是“a”的值会临时存放在内存中,后续的where条件匹配时磁盘数据库中没有“a”列,匹配不到就会报错.

having的条件匹配是针对内存中的结果集.

where发挥的比较早,是针对内存中的表发挥的作用,筛选出结果集

从载从结果集中进一步筛选,需要用到having


where和having都存在,where要放在having的前面;

因为where是对内存中的表进行筛选

而having是对进一步计算出来的结果集进行再一步筛选

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容