mysql> #查询每个栏目最贵的商品
mysql> select goods_id,shop_price,cat_id from (select goods_id,shop_price,cat_id
from goods order by shop_price desc) as temp group by cat_id;
+----------+------------+--------+
| goods_id | shop_price | cat_id |
+----------+------------+--------+
| 16 | 823.33 | 2 |
| 22 | 5999.00 | 3 |
| 18 | 2878.00 | 4 |
| 23 | 3700.00 | 5 |
| 7 | 100.00 | 8 |
| 6 | 42.00 | 11 |
| 25 | 48.00 | 13 |
| 29 | 90.00 | 14 |
| 27 | 95.00 | 15 |
+----------+------------+--------+
9 rows in set (0.02 sec) mysql> #查询出每个栏目最低的商品
mysql> select goods_id,shop_price,cat_id from(select goods_id,shop_price,cat_id
from goods order by shop_price asc) as temp group by cat_id;
+----------+------------+--------+
| goods_id | shop_price | cat_id |
+----------+------------+--------+
| 16 | 823.33 | 2 |
| 20 | 280.00 | 3 |
| 1 | 1388.00 | 4 |
| 23 | 3700.00 | 5 |
| 4 | 58.00 | 8 |
| 5 | 20.00 | 11 |
| 26 | 19.00 | 13 |
| 30 | 18.00 | 14 |
| 28 | 45.00 | 15 |
+----------+------------+--------+
9 rows in set (0.00 sec) mysql> #查询出每个栏目的平均价格
mysql> select goods_id,cat_id,shop_price from goods where cat_id=2;
+----------+--------+------------+
| goods_id | cat_id | shop_price |
+----------+--------+------------+
| 16 | 2 | 823.33 |
+----------+--------+------------+
1 row in set (0.00 sec) mysql> select goods_id,cat_id,shop_price from goods where cat_id=3;
+----------+--------+------------+
| goods_id | cat_id | shop_price |
+----------+--------+------------+
| 8 | 3 | 399.00 |
| 9 | 3 | 2298.00 |
| 10 | 3 | 1328.00 |
| 11 | 3 | 1300.00 |
| 12 | 3 | 983.00 |
| 13 | 3 | 1311.00 |
| 15 | 3 | 788.00 |
| 17 | 3 | 2300.00 |
| 19 | 3 | 858.00 |
| 20 | 3 | 280.00 |
| 21 | 3 | 2000.00 |
| 22 | 3 | 5999.00 |
| 24 | 3 | 2000.00 |
| 31 | 3 | 1337.00 |
| 32 | 3 | 3010.00 |
+----------+--------+------------+
15 rows in set (0.00 sec) mysql> select cat_id,avg(shop_price) from goods where cat_id=3;
+--------+-----------------+
| cat_id | avg(shop_price) |
+--------+-----------------+
| 3 | 1746.066667 |
+--------+-----------------+
1 row in set (0.00 sec) mysql> select cat_id from goods group by cat_id;
+--------+
| cat_id |
+--------+
| 2 |
| 3 |
| 4 |
| 5 |
| 8 |
| 11 |
| 13 |
| 14 |
| 15 |
+--------+
9 rows in set (0.00 sec) mysql> select cat_id,avg(shop_price) from goods where cat_id in (
-> select cat_id from goods group by cat_id);
+--------+-----------------+
| cat_id | avg(shop_price) |
+--------+-----------------+
| 4 | 1232.526774 |
+--------+-----------------+
1 row in set (0.03 sec) mysql> select cat_id,sum(goods_number) from goods where cat_id=4;
+--------+-------------------+
| cat_id | sum(goods_number) |
+--------+-------------------+
| 4 | 3 |
+--------+-------------------+
1 row in set (0.00 sec) mysql> select cat_id,sum(goods_number) from goods group by cat_id;
+--------+-------------------+
| cat_id | sum(goods_number) |
+--------+-------------------+
| 2 | 0 |
| 3 | 203 |
| 4 | 3 |
| 5 | 8 |
| 8 | 61 |
| 11 | 23 |
| 13 | 4 |
| 14 | 9 |
| 15 | 2 |
+--------+-------------------+
9 rows in set (0.00 sec) mysql> #取出每个栏目下的商品的平均价格
mysql> select cat_id,avg(shop_price) as '平均价' from goods group by cat_id;
+--------+-------------+
| cat_id | 平均价 |
+--------+-------------+
| 2 | 823.330000 |
| 3 | 1746.066667 |
| 4 | 2297.000000 |
| 5 | 3700.000000 |
| 8 | 75.333333 |
| 11 | 31.000000 |
| 13 | 33.500000 |
| 14 | 54.000000 |
| 15 | 70.000000 |
+--------+-------------+
9 rows in set (0.03 sec) mysql> #取出每个栏目下的库存量
mysql> select cat_id,goods_number from goods group by cat_id;
+--------+--------------+
| cat_id | goods_number |
+--------+--------------+
| 2 | 0 |
| 3 | 1 |
| 4 | 1 |
| 5 | 8 |
| 8 | 17 |
| 11 | 8 |
| 13 | 2 |
| 14 | 0 |
| 15 | 2 |
+--------+--------------+
9 rows in set (0.00 sec) mysql> #查询每个栏目下商品的种类
mysql> select cat_id,count(*) as '商品种类' from goods cat_id;
+--------+----------+
| cat_id | 商品种类 |
+--------+----------+
| 4 | 31 |
+--------+----------+
1 row in set (0.00 sec) mysql> select cat_id,count(*) as '商品种类' from goods group by cat_id;
+--------+----------+
| cat_id | 商品种类 |
+--------+----------+
| 2 | 1 |
| 3 | 15 |
| 4 | 3 |
| 5 | 1 |
| 8 | 3 |
| 11 | 2 |
| 13 | 2 |
| 14 | 2 |
| 15 | 2 |
+--------+----------+
9 rows in set (0.02 sec) mysql> #having和group综合使用查询
mysql> #查询该店的商品比市场价所省的价格 mysql> select goods_id,shop_price,market_price-shop_price as '比市场省的钱' from
goods;
+----------+------------+--------------+
| goods_id | shop_price | 比市场省的钱 |
+----------+------------+--------------+
| 1 | 1388.00 | 277.60 |
| 4 | 58.00 | 11.60 |
| 3 | 68.00 | 13.60 |
| 5 | 20.00 | 4.00 |
| 6 | 42.00 | 8.40 |
| 7 | 100.00 | 20.00 |
| 8 | 399.00 | 79.79 |
| 9 | 2298.00 | 459.60 |
| 10 | 1328.00 | 265.60 |
| 11 | 1300.00 | -1300.00 |
| 12 | 983.00 | 196.60 |
| 13 | 1311.00 | 262.20 |
| 14 | 2625.00 | 525.00 |
| 15 | 788.00 | 157.60 |
| 16 | 823.33 | 164.67 |
| 17 | 2300.00 | 460.00 |
| 18 | 2878.00 | 575.60 |
| 19 | 858.00 | 171.60 |
| 20 | 280.00 | 56.00 |
| 21 | 2000.00 | 400.00 |
| 22 | 5999.00 | 1199.80 |
| 23 | 3700.00 | 740.00 |
| 24 | 2000.00 | 400.00 |
| 25 | 48.00 | 9.59 |
| 26 | 19.00 | 3.80 |
| 27 | 95.00 | 5.00 |
| 28 | 45.00 | 5.00 |
| 29 | 90.00 | -90.00 |
| 30 | 18.00 | 3.00 |
| 31 | 1337.00 | 267.39 |
| 32 | 3010.00 | 602.00 |
+----------+------------+--------------+
31 rows in set (0.05 sec) mysql> #查询每个商品所积压的货款
mysql> select goods_id,goods_number*shop_price as '积压的货款' from goods;
+----------+------------+
| goods_id | 积压的货款 |
+----------+------------+
| 1 | 1388.00 |
| 4 | 986.00 |
| 3 | 1632.00 |
| 5 | 160.00 |
| 6 | 630.00 |
| 7 | 2000.00 |
| 8 | 399.00 |
| 9 | 9192.00 |
| 10 | 9296.00 |
| 11 | 1300.00 |
| 12 | 7864.00 |
| 13 | 10488.00 |
| 14 | 2625.00 |
| 15 | 2364.00 |
| 16 | 0.00 |
| 17 | 2300.00 |
| 18 | 2878.00 |
| 19 | 10296.00 |
| 20 | 3360.00 |
| 21 | 80000.00 |
| 22 | 5999.00 |
| 23 | 29600.00 |
| 24 | 200000.00 |
| 25 | 96.00 |
| 26 | 38.00 |
| 27 | 190.00 |
| 28 | 0.00 |
| 29 | 0.00 |
| 30 | 162.00 |
| 31 | 1337.00 |
| 32 | 12040.00 |
+----------+------------+
31 rows in set (0.01 sec) mysql> #查询积压的总货款
mysql> select sum(shop_price*goods_number) as '积压的总货款' from goods;
+--------------+
| 积压的总货款 |
+--------------+
| 398620.00 |
+--------------+
1 row in set (0.00 sec) mysql> #查询每个栏目积压的货款
mysql> select cat_id,sum(shop_price*goods_number) as '积压的货款' from goods gro
up by cat_id;
+--------+------------+
| cat_id | 积压的货款 |
+--------+------------+
| 2 | 0.00 |
| 3 | 356235.00 |
| 4 | 6891.00 |
| 5 | 29600.00 |
| 8 | 4618.00 |
| 11 | 790.00 |
| 13 | 134.00 |
| 14 | 162.00 |
| 15 | 190.00 |
+--------+------------+
9 rows in set (0.00 sec) mysql> #查询比市场价省钱200元以上的商品及该商品所省的钱
mysql> select goods_id,market_price-shop_price as 'sheng' from goods where marke
t_price-shop_price>200;
+----------+---------+
| goods_id | sheng |
+----------+---------+
| 1 | 277.60 |
| 9 | 459.60 |
| 10 | 265.60 |
| 13 | 262.20 |
| 14 | 525.00 |
| 17 | 460.00 |
| 18 | 575.60 |
| 21 | 400.00 |
| 22 | 1199.80 |
| 23 | 740.00 |
| 24 | 400.00 |
| 31 | 267.39 |
| 32 | 602.00 |
+----------+---------+
13 rows in set (0.12 sec) mysql> select goods_id,market_price-shop_price as 'sheng' from goods having shen
g>200;
+----------+---------+
| goods_id | sheng |
+----------+---------+
| 1 | 277.60 |
| 9 | 459.60 |
| 10 | 265.60 |
| 13 | 262.20 |
| 14 | 525.00 |
| 17 | 460.00 |
| 18 | 575.60 |
| 21 | 400.00 |
| 22 | 1199.80 |
| 23 | 740.00 |
| 24 | 400.00 |
| 31 | 267.39 |
| 32 | 602.00 |
+----------+---------+
13 rows in set (0.00 sec) mysql> #查询货款大于2w的栏目,以及该栏目积压的货款
mysql> select cat_id,sum(goods_number*shop_price) as huokuan from goods group by
cat_id having huokuan>20000;
+--------+-----------+
| cat_id | huokuan |
+--------+-----------+
| 3 | 356235.00 |
| 5 | 29600.00 |
+--------+-----------+
2 rows in set (0.01 sec) mysql>

mysql查询练习的更多相关文章

  1. mysql查询性能优化

    mysql查询过程: 客户端发送查询请求. 服务器检查查询缓存,如果命中缓存,则返回结果,否则,继续执行. 服务器进行sql解析,预处理,再由优化器生成执行计划. Mysql调用存储引擎API执行优化 ...

  2. Mysql查询——深入学习

    1.开篇 之前上一篇的随笔基本上是单表的查询,也是mysql查询的一个基本.接下来我们要看看两个表以上的查询如何得到我们想要的结果. 在学习的过程中我们一起进步,成长.有什么写的不对的还望可以指出. ...

  3. Mysql 查询练习

    Mysql 查询练习 ---创建班级表 create table class( cid int auto_increment primary key, caption ) )engine=innodb ...

  4. mysql 查询去重 distinct

    mysql 查询去重 distinct   待完善内容..

  5. MySQl查询区分大小写的解决办法

    通过查询资料发现需要设置collate(校对) . collate规则: *_bin: 表示的是binary case sensitive collation,也就是说是区分大小写的 *_cs: ca ...

  6. 【转】mysql查询结果输出到文件

    转自:http://www.cnblogs.com/emanlee/p/4233602.html mysql查询结果导出/输出/写入到文件 方法一: 直接执行命令: mysql> select ...

  7. MySQL查询缓存

    MySQL查询缓存 用于保存MySQL查询语句返回的完整结果,被命中时,MySQL会立即返回结果,省去解析.优化和执行等阶段. 如何检查缓存? MySQL保存结果于缓存中: 把SELECT语句本身做h ...

  8. mysql 查询数据时按照A-Z顺序排序返回结果集

    mysql 查询数据时按照A-Z顺序排序返回结果集 $sql = "SELECT * , ELT( INTERVAL( CONV( HEX( left( name, 1 ) ) , 16, ...

  9. MySQL查询今天/本周/上周/本月/上个月份的数据

    MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周.上周.本月.上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看. 查询当前今天的数据 SELECT name,sub ...

  10. [转]向facebook学习,通过协程实现mysql查询的异步化

    FROM : 通过协程实现mysql查询的异步化 前言 最近学习了赵海平的演讲,了解到facebook的mysql查询可以进行异步化,从而提高性能.由于facebook实现的比较早,他们不得不对php ...

随机推荐

  1. WCF大数据量传输解决方案

    文章内容列表:1. 场景:2. 解决方案3. WCF契约与服务实现设计静态图4. WCF契约与服务实现设计详细说明6. 服务端启动服务代码:7. 客户端代码8.   WCF大数据量传输解决方案源码下载 ...

  2. C# 操作 Word 修改word的高级属性中的自定义属性

    为 Microsoft Word 创建自动化客户端 启动 Visual Studio .NET. 在文件菜单上,单击新建,然后单击项目.从 Visual C# 项目类型中选择 Windows 应用程序 ...

  3. BrnShop开源网上商城第一讲:架构设计

    首先在此感谢大家对BrnShop项目的支持和鼓励!我们在发布BrnShop以前曾推测项目会受到不少园友的支持,但没想到园友们的支持大大超过我们的预测.4天6000次浏览,140个推荐,170个评论,8 ...

  4. (转载)php array_merge 和 两数组相加区别

    (转载)http://www.cnblogs.com/shistou/archive/2013/03/16/2963586.html PHP中两个数组合并可以使用+或者array_merge,但之间还 ...

  5. JZ2440开发笔记(5)——通过按键点亮LED

    在JZ2440中,点亮LED就是给LED的控制位设置为输出,数据位设置为低电平,而通过按键点亮LED,就需要将按键对应的控制位设置为输出. 下面是JZ2440的3个LED电路图: 下面是JZ2440的 ...

  6. bzoj 2707 [SDOI2012]走迷宫(SCC+高斯消元)

    Description Morenan被困在了一个迷宫里.迷宫可以视为N个点M条边的有向图,其中Morenan处于起点S,迷宫的终点设为T.可惜的是,Morenan非常的脑小,他只会从一个点出发随机沿 ...

  7. Vijos P1060 盒子

    Vijos P1060 盒子 链接:https://vijos.org/p/1060 [思路] 组合公式+精度选择. 首先解决将A个数放入N个集合的数目,其中集合可空.因为可以有球不放入集合,所以增加 ...

  8. hdu3394--Railway(点的双连通分量)

    一个公园中有 n 个景点,景点之间通过无向的道路来连接,如果至少两个环公用一条路,路上的游客就会发生冲突:如果一条路不属于任何的环,这条路就没必要修 问,有多少路不必修,有多少路会发生冲突 每一个连通 ...

  9. What does enumerable mean?

    I was directed to MDN's for..in page when it said, "for..in Iterates over the enumerable proper ...

  10. js http 请求 多个相同参数名传值

    最近在用js和api做对接的时候需要传参数类似于 rights=a1&rights=a2 因为有相同的参数名,试过很多方法都被覆盖了. 最后终于发现可以通过rights=[a1,a2]的方式, ...