MySQL 的查询

MySQL 的查询

mysql查询的几种子语句方法

where(条件查询)、having(筛选)、group by(分组)、order by(排序)、limit(限制结果数)

1、group by 分组

一般情况下group需与统计函数(聚合函数)一起使用才有意义
如:select goods_id,goods_name,cat_id,max(shop_price) from goods group by cat_id;
这里取出来的结果中的good_name是错误的!因为shop_price使用了max函数,那么它是取最大的,而语句中使用了group by 分组,那么goods_name并没有使用聚合函数,它只是cat_id下的第一个商品,并不会因为shop_price改变而改变
mysql中的五种统计函数:
(1)max:求最大值
select max(goods_price) from goods
这里会取出最大的价格的值,只有值
#查询每个栏目下价格最高的
select cat_id,max(goods_price) from goods group by cat_id;
#查出价格最高的商品编号
select goods_id,max(goods_price) from goods group by goods_id;

            (2)min:求最小值
            (3)sum:求总数和
                    #求商品库存总和
                    select sum(goods_number) from goods;
            (4)avg:求平均值
                    #求每个栏目的商品平均价格
                    select cat_id,avg(goods_price) from goods group by cat_id;
            (5)count:求总行数
                    #求每个栏目下商品种类
                    select cat_id,count(*) from goods group by cat_id;

2、where常用运算符:
逻辑运算符
not ( ! ) 逻辑非
or ( || ) 逻辑或
and ( && ) 逻辑与
比较运算符
> , < ,= , != (< >),>= , <=
in(v1,v2…vn)
between v1 and v2 在v1至v2之间(包含v1,v2)

            where price>=1000 and price <= 3000 or price >=200 and price <=500
            取200-500或者1000-3000的值
            where price not between 1000 and 3000
            不在1000与3000之间的值

        模糊查询
            like 像
            通配符:
            %  任意字符
            _   单个字符
                where goods_name like '华为%'
                where goods_name like '华为PLUS__'

3、limit
limit [offset,] N
offset 偏移量,可选,不写则相当于limit 0,N
N 取出条目

                #取价格第4-6高的商品
                select good_id,goods_name,goods_price from goods order by good_price desc limit 3,3;
                
        ###查询每个栏目下最贵的商品
            思路:
                    #先对每个栏目下的商品价格排序
                    select cat_id,goods_id,goods_name,shop_price from goods order by cat_id,shop_price desc;
                    #上面的查询结果中每个栏目的第一行的商品就是最贵的商品
                    #把上面的查询结果理解为一个临时表[存在于内存中]【子查询】
                    #再从临时表中选出每个栏目最贵的商品
                    select * from (select goods_id,goods_name,cat_id,shop_price from goods order by cat_id,shop_price desc) as t group by cat_id;
                    #这里使用group by cat_id是因为临时表中每个栏目的第一个商品就是最贵的商品,而group by前面没有使用聚合函数,所以默认就取每个分组的第一行数据,这里以cat_id分组

             良好的理解模型:
                1、where后面的表达式,把表达式放在每一行中,看是否成立
                2、字段(列),理解为变量,可以进行运算(算术运算和逻辑运算)  
                3、 取出结果可以理解成一张临时表

4、having 与where 的异同点

                having与where类似,可以筛选数据,where后的表达式怎么写,having后就怎么写
                where针对表中的列发挥作用,查询数据
                having对查询结果中的列发挥作用,筛选数据
                #查询本店商品价格比市场价低多少钱,输出低200元以上的商品
                select goods_id,good_name,market_price - shop_price as s from goods having s>200 ;
                //这里不能用where因为s是查询结果,而where只能对表中的字段名筛选
                如果用where的话则是:
                select goods_id,goods_name from goods where market_price - shop_price > 200;

                #同时使用where与having
                select cat_id,goods_name,market_price - shop_price as s from goods where cat_id = 3 having s > 200;
                #查询积压货款超过2万元的栏目,以及该栏目积压的货款
                select cat_id,sum(shop_price * goods_number) as t from goods group by cat_id having s > 20000
                #查询两门及两门以上科目不及格的学生的平均分
                      思路:
                        #先计算所有学生的平均分
                         select name,avg(score) as pj from stu group by name;
                        #查出所有学生的挂科情况
                        select name,score<60 from stu;
                                #这里score<60是判断语句,所以结果为真或假,mysql中真为1假为0
                        #查出两门及两门以上不及格的学生
                        select name,sum(score<60) as gk from stu group by name having gk > 1;
                        #综合结果
                        select name,sum(score<60) as gk,avg(score) as pj from stu group by name having gk >1;

5、order by
(1) order by price //默认升序排列
(2)order by price desc //降序排列
(3)order by price asc //升序排列,与默认一样
(4)order by rand() //随机排列,效率不高
#按栏目号升序排列,每个栏目下的商品价格降序排列
select * from goods where cat_id !=2 order by cat_id,price desc;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值