mysql的查询、子查询及连接查询分析 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

   

    一、mysql查询的五种子句

    where(条件查询)、having(筛选)、group by(分组)、order by(排序)、

    limit(限制结果数)

    1、where常用运算符:

    比较运算符

    > , < ,= , != (< >),>= , <=

    in(v1,v2..vn)

    between v1 and v2 在v1至v2之间(包含v1,v2)

    逻辑运算符

    not ( ! ) 逻辑非

    or ( || ) 逻辑或

    and ( && ) 逻辑与

    where price>=3000 and price <= 5000 or price >=500 and price <=1000

    取500-1000或者3000-5000的值

    where price not between 3000 and 5000

    不在3000与5000之间的值

    模糊查询

    like 像

    通配符:

    % 任意字符

    _ 单个字符

    where goods_name like '诺基亚%'

    where goods_name like '诺基亚N__'

    2、group by 分组 www.hanwangtx.com

    一般情况下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 goos 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;

    ###要把每个字段名当成变量来理解,它可以进行运算###

    例:查询本店每个商品价格比市场价低多少;

    select goods_id,goods_name,goods_price-market_price from goods;

    查询每个栏目下面积压的货款

    select cat_id,sum(goods_price*goods_number) from goods

    group by cat_id;

    ###可以用as来给计算结果取个别名###

    select cat_id,sum(goods_price * goods_number) as hk from

    goods group by cat_id

    不仅列名可以取别名,表单也可以取别名 www.hanwangtx.com

    3、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;

    4、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;

    5、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; www.2cto.com

    ###查询每个栏目下最贵的商品

    思路:

    #先对每个栏目下的商品价格排序

最新文章