美文网首页
mysql中基础练习

mysql中基础练习

作者: Arnold_Z | 来源:发表于2016-11-25 14:28 被阅读169次

练习用的表 表结构

把num值处于[20,29]之间,改为20;num值处于[30,39]之间的,改为30。

方法一:

 update mian set num = 20 where num between 20 and 29;
 update mian set num = 30 where num between 30 and 39;

方法二:

update mian set num = floor(num/10)*10 where num between 20 and 39;
改后结果
练习用的表 表结构

1、基础查询where的练习:

1.1、主键为32的商品

select goods_id, goods_name, shop_price,market_price from goods where goods_id = 32;
图1-1

1.2、不属第3栏目的所有商品

select goods_id, goods_name, cat_id, shop_price,market_price from goods where cat_id != 3;
图1-2

1.3、本店价格高于3000元的商品

select goods_id, goods_name, cat_id, shop_price,market_price from goods where shop_price > 3000;
图1-3

1.4、本店价格低于或等于100元的商品

select goods_id, goods_name, cat_id, shop_price,market_price from goods where shop_price < 100;
图1-4

1.5、取出第4栏目或第11栏目的商品(不许用or)

select goods_id, goods_name, cat_id, shop_price,market_price from goods where cat_id in (4,11);
图1-5

1.6、取出100<=价格<=500的商品(不许用and)

select goods_id, goods_name, cat_id, shop_price,market_price from goods where shop_price between 100 and 500;
图1-6

1.7、取出不属于第3栏目且不属于第11栏目的商品(and,或not in分别实现)

 select goods_id, goods_name, cat_id, shop_price,market_price from goods where cat_id !=3 and cat_id !=11;
 select goods_id, goods_name, cat_id, shop_price,market_price from goods where cat_id not in (3, 11);
图1-7

1.8、取出价格大于100且小于300,或者大于4000且小于5000的商品

select goods_id,cat_id,goods_name,shop_price  from goods where shop_price > 100 and shop_price < 300 or shop_price > 4000 and shop_price < 5000;
图1-8

1.9、取出第3个栏目下面价格<1000或>3000,并且点击量>5的系列商品

select goods_id,cat_id,goods_name,shop_price,click_count from goods where cat_id = 3 and (shop_price < 1000 or shop_price > 3000) and click_count > 5;
图1-9

1.10、取出第1个栏目下面的商品(注意:1栏目下面没商品,但其子栏目下有)
栏目表如下图:

本题所说的栏目表

题目中要取出栏目为1的商品,即cat_id为1的goods表中的商品。从表中可知cat_id 2~5的栏目均属于cat_id = 1的子栏目,所以这些要取出来。

# 建立一个视图保存从goods表中匹对出第1栏中的子栏商品
create view catid_1 as
-> select goods.cat_id, goods_name,category.parent_id, cat_name
-> from goods right join category
-> on goods.cat_id = category.cat_id and category.parent_id = 1;
图1-10-1 view视图
# 选出parent_id为1的商品即可
select parent_id, cat_id, goods_name, cat_name from catid_1 where parent_id = 1;
图1-10-2 最终结果

1.11、取出名字以"诺基亚"开头的商品

select goods_id,cat_id,goods_name,shop_price from goods where goods_name like '诺基亚%';
图1-11

1.12、取出名字为"诺基亚Nxx"的手机

 select goods_id,cat_id,goods_name,shop_price from goods where goods_name like '诺基亚N__';
图1-12

1.13、取出名字不以"诺基亚"开头的商品

select goods_id,cat_id,goods_name,shop_price from goods where goods_name not like '诺基亚%';
图1-13

1.14、取出第3个栏目下面价格在1000到3000之间,并且点击量>5 "诺基亚"开头的系列商品

select goods_id,cat_id,goods_name,shop_price from goods where cat_id = 3 and (shop_price between 1000 and 3000) and goods_name like '诺基亚%';
图1-14

1.15、把goods表中商品名为'诺基亚xxxx'的商品,改为'HTCxxxx',提示:大胆的把列看成变量,参与运算,甚至调用函数来处理 .(substring(),concat())

update goods set goods_name = concat('HTC',substring(goods_name,4)) where goods_name like '诺基亚%';
图1-15

2、分组查询group:
2.1、查出最贵的商品的价格

select max(shop_price) from goods;
图2-1

2.2、查出最大(最新)的商品编号

select max(goods_id) from goods;
图2-2

2.3、查出最便宜的商品的价格

select min(shop_price) from goods;
图2-3

2.4、查出最旧(最小)的商品编号

select min(goods_id) from goods;
图2-4

2.5、查询该店所有商品的库存总量

select sum(goods_number) from goods;
图2-5

2.6、查询所有商品的平均价

select avg(shop_price) from goods;
图2-6

2.7、查询该店一共有多少种商品

select count(*) from goods;
图2-7

2.8、查询每个栏目下面
最贵商品价格
最低商品价格
商品平均价格
商品库存量
商品种类
提示:(5个聚合函数,sum,avg,max,min,count与group综合运用)

select cat_id, max(shop_price),min(shop_price), avg(shop_price), sum(goods_number),count(goods_id) from goods group by cat_id;
图2-8

3、having与group综合运用查询:
3.1、查询该店的商品比市场价所节省的价格

select goods_id, goods_name, market_price - shop_price as safe_money from goods;
图3-1

3.2、查询每个商品所积压的货款(提示:库存*单价)

select goods_id, goods_name, goods_number * shop_price as loan from goods;
图3-2

3.3、查询该店积压的总货款

select sum(goods_number * shop_price) as loan_all from goods;
图3-3

3.4、查询该店每个栏目下面积压的货款.

select cat_id, sum(goods_number * shop_price) as loan_all from goods group by cat_id;
图3-4

3.5、查询比市场价省钱200元以上的商品及该商品所省的钱(where和having分别实现)
where:

select goods_id, goods_name, shop_price, market_price, market_price - shop_price as safe_money from goods where (market_price - shop_price) > 200;

having:

select goods_id, goods_name, shop_price, market_price, market_price - shop_price as safe_money from goods having safe_money > 200;
图3-5

3.6、查询积压货款超过2W元的栏目,以及该栏目积压的货款

select cat_id, sum(goods_number * shop_price) as loan_all from goods group by cat_id having loan_all > 20000;
图3-6

3.7、where-having-group综合练习题

图3-7-1 所用表格

要求:查询出2门及2门以上不及格者的平均成绩。

select name, avg(score) as avg_score, sum(score < 60) as fail_courses from result
group by name
having fail_courses >= 2;
图3-7-2

Ps:这里很容易出错,一开始我自己就错用count( )来统计不及格成绩数,但是count( )只能计算行数,及格数也占一行,所以统计出来的数量是错误的。另外,sql思路与以往有所区别,要什么就先取什么,然后再一步步筛选。


相关文章

网友评论

      本文标题:mysql中基础练习

      本文链接:https://www.haomeiwen.com/subject/eraipttx.html