mysql查询练习

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>
时间: 2024-10-13 23:57:45

mysql查询练习的相关文章

MySQL 查询语句使用进阶

MySQL 查询语句使用进阶 =============================================================================== 概述: =============================================================================== 练习: 练习1  首先导入hellodb.sql的脚本文件,查询其数据库和表如下: [[email protected] ~]# mysql 

Mysql查询缓存碎片、缓存命中率及Nagios监控

Mysql 的优化方案,在互联网上可以查找到非常多资料,今天对Mysql缓存碎片和命中率作了详细了解,个人作了简单整理. 一.Mysql查询缓存碎片和缓存命中率. mysql> SHOW STATUS LIKE 'qcache%'; +-------------------------+-----------+ | Variable_name | Value | +-------------------------+-----------+ | Qcache_free_blocks | 5 |

MySQL查询in操作 查询结果按in集合顺序显示_Mysql_脚本之家

body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI",Tahoma,Helvetica,Sans-Serif,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5;

MySQL查询计划输出列的含义

"一:MySQL查询计划输出列的含义:1.id:每个被独立执行的操作的标识,表示对象被操作的顺序:id值越大,先被执行:如果相同,执行顺序从上到下.2.select_type:查询中每个select子句的类型.3.table:名字,被操作的对象的名称,通常是表名,但有其他格式.4.partitions:匹配的分区信息(对于非分区表值为NULL).5.type:连接操作的类型.6.possible_keys:备选的索引(列出可能被使用到的索引).7.key:经优化器选定的索引:常使用ANALYZE

mysql查询缓存打开、设置、参数查询、性能变量意思

http://blog.sina.com.cn/s/blog_75ad10100101by7j.html http://www.cnblogs.com/zemliu/archive/2013/08/03/3234372.html 第一: query_cache_type 使用查询缓存的方式 一般,我们会把 query_cache_type 设置为 ON,默认情况下应该是ON mysql> select @@query_cache_type;+--------------------+| @@qu

MySQL查询本周、上周、本月、上个月份数据的sql代码(转)

感谢:http://www.jb51.net/article/32277.htm ------------------------------------------------------------------------------ MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周.上周.本月.上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看 查询当前这周的数据 SELECT name,submittime FROM enterprise WHERE Y

Mysql 查询注意和执行shell命令

Mysql 查询注意 1. 在mysql查询的时候需要注意在表的前面加上数据库的前缀,不然就是默认是当前的数据库(当多个库查询的时候,可能会出现重复的查相同的表多次) 2. \! ls –al ,mysql可以执行shell 外部的命令(注意\!和ls之间的空格) 版权声明:本文为博主原创文章,未经博主允许不得转载.

MYSQL查询性能优化

查询的基础知识 MySQL查询过程如下图所示: MySQL是通过查询语句的哈希查找来命中缓存的,需要注意的是如果查询语句大小写不一致或者有多余的空格,是不会命中缓存的. 一个查询通常有很多执行方式,查询优化器通过计算开销(随机读取次数)来选择最优的查询. MySQL把所以的查询都当做联接来处理,联接是按照循环嵌套的策略来执行的,如下图所示: 查询的优化和限制 我们需要知道查询优化器会做哪些优化,这样在写查询的时候就可以不需要考虑手动来做这些优化,把这些事情交给查询优化器去做是更好的选择,查询优化

mysql 查询当天、昨天、本周、上周、本月、上月、今年、去年数据

mysql 查询当天数据 mysql查询今天.昨天.7天.近30天.本月.上一月 数据 今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) = 1 近7天 SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名) 查询当