mysql的having语句

mysql> use qq;

Database changed

mysql> #查询本店价比市场价省的钱,并且要求省钱200元以上的取出来

mysql> select goods_id,market_price,shop_price,(market_price-shop_price) as discount from goods where having discount>200;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'having discount>200' at line 1

mysql> select goods_id,market_price,shop_price,(market_price-shop_price) as discount from goods having discount>200;

+----------+--------------+------------+----------+

| goods_id | market_price | shop_price | discount |

+----------+--------------+------------+----------+

|        1 |      1665.60 |    1388.00 |   277.60 |

|        9 |      2757.60 |    2298.00 |   459.60 |

|       10 |      1593.60 |    1328.00 |   265.60 |

|       13 |      1573.20 |    1311.00 |   262.20 |

|       14 |      3150.00 |    2625.00 |   525.00 |

|       17 |      2760.00 |    2300.00 |   460.00 |

|       18 |      3453.60 |    2878.00 |   575.60 |

|       21 |      2400.00 |    2000.00 |   400.00 |

|       22 |      7198.80 |    5999.00 |  1199.80 |

|       23 |      4440.00 |    3700.00 |   740.00 |

|       24 |      2400.00 |    2000.00 |   400.00 |

|       31 |      1604.39 |    1337.00 |   267.39 |

|       32 |      3612.00 |    3010.00 |   602.00 |

+----------+--------------+------------+----------+

13 rows in set (0.01 sec)



mysql> #where和having不能同时出现

mysql> #查询出每种商品所积压的货款

mysql> select goods_id,shop_price,goods_number,shop_price*goods_number as jy from goods;

+----------+------------+--------------+-----------+

| goods_id | shop_price | goods_number | jy        |

+----------+------------+--------------+-----------+

|        1 |    1388.00 |            1 |   1388.00 |

|        4 |      58.00 |           17 |    986.00 |

|        3 |      68.00 |           24 |   1632.00 |

|        5 |      20.00 |            8 |    160.00 |

|        6 |      42.00 |           15 |    630.00 |

|        7 |     100.00 |           20 |   2000.00 |

|        8 |     399.00 |            1 |    399.00 |

|        9 |    2298.00 |            4 |   9192.00 |

|       10 |    1328.00 |            7 |   9296.00 |

|       11 |    1300.00 |            1 |   1300.00 |

|       12 |     983.00 |            8 |   7864.00 |

|       13 |    1311.00 |            8 |  10488.00 |

|       14 |    2625.00 |            1 |   2625.00 |

|       15 |     788.00 |            3 |   2364.00 |

|       16 |     823.33 |            0 |      0.00 |

|       17 |    2300.00 |            1 |   2300.00 |

|       18 |    2878.00 |            1 |   2878.00 |

|       19 |     858.00 |           12 |  10296.00 |

|       20 |     280.00 |           12 |   3360.00 |

|       21 |    2000.00 |           40 |  80000.00 |

|       22 |    5999.00 |            1 |   5999.00 |

|       23 |    3700.00 |            8 |  29600.00 |

|       24 |    2000.00 |          100 | 200000.00 |

|       25 |      48.00 |            2 |     96.00 |

|       26 |      19.00 |            2 |     38.00 |

|       27 |      95.00 |            2 |    190.00 |

|       28 |      45.00 |            0 |      0.00 |

|       29 |      90.00 |            0 |      0.00 |

|       30 |      18.00 |            9 |    162.00 |

|       31 |    1337.00 |            1 |   1337.00 |

|       32 |    3010.00 |            4 |  12040.00 |

+----------+------------+--------------+-----------+

31 rows in set (0.00 sec)



mysql> #查询该店积压的总货款,每个商品的积压货款之和

mysql> select sum(goods_number*shop_price) from goods;

+------------------------------+

| sum(goods_number*shop_price) |

+------------------------------+

|                    398620.00 |

+------------------------------+

1 row in set (0.00 sec)



mysql> #查询每个栏目下的积压货款

mysql> select cat_id,sum(shop_price*goods_number) from goods group by cat_id;

+--------+------------------------------+

| cat_id | sum(shop_price*goods_number) |

+--------+------------------------------+

|      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> #查询出栏目积压货款,,并筛选出积压货款>20000的栏目

mysql> select cat_id,sum(goods_number*shop_price) as jhk

    -> from goods group by cat_id

    -> having jhk>20000;

+--------+-----------+

| cat_id | jhk       |

+--------+-----------+

|      3 | 356235.00 |

|      5 |  29600.00 |

+--------+-----------+

2 rows in set (0.00 sec)



mysql> create table result(

    -> name varchar(20) default null,

    -> subject varchar(20) default null,

    -> score tinyint(4) default null

    -> )engine myisam charset utf8;

Query OK, 0 rows affected (0.08 sec)



mysql> insert into result{

    -> \c

mysql> insert into result

    -> values

    -> ('张三','数学',90),

    -> ('张三','语文',50),

    -> ('张三','地理',40)

    -> ;

ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'name' at row 1

mysql> set names gnk;

ERROR 1115 (42000): Unknown character set: 'gnk'

mysql> set names gbk;

Query OK, 0 rows affected (0.00 sec)



mysql> insert into result

    -> values

    -> ('张三','数学',90),

    -> ('张三','语文',50),

    -> ('张三','地理',40);

Query OK, 3 rows affected (0.02 sec)

Records: 3  Duplicates: 0  Warnings: 0



mysql> insert into result

    -> values

    -> ('','',55),

    -> ('张三','数学',90),\c

mysql> insert into result

    -> values

    -> ('李四','语文',55),

    -> ('李四','政治',45),

    -> ('王五','政治',30);

Query OK, 3 rows affected (0.00 sec)

Records: 3  Duplicates: 0  Warnings: 0



mysql> select * from result;

+------+---------+-------+

| name | subject | score |

+------+---------+-------+

| 张三     | 数学       |    90 |

| 张三     | 语文        |    50 |

| 张三     | 地理        |    40 |

| 李四     | 语文        |    55 |

| 李四     | 政治        |    45 |

| 王五     | 政治        |    30 |

+------+---------+-------+

6 rows in set (0.03 sec)



mysql> #查询2门及2门以上不及格者的平均成绩

mysql> select name,avg(subject) form result group by name having subject<60;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'result group by name having subject<60' at line 1

mysql> select name,avg(subject) form result group by name having score<60;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'result group by name having score<60' at line 1

mysql> select name,avg(score) form result group by name where score<60;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'result group by name where score<60' at line 1

mysql> select name,score,score<60 from result group by name;

+------+-------+----------+

| name | score | score<60 |

+------+-------+----------+

| 张三     |    90 |        0 |

| 李四     |    55 |        1 |

| 王五     |    30 |        1 |

+------+-------+----------+

3 rows in set (0.02 sec)



mysql> select name,score,score<60 from result;

+------+-------+----------+

| name | score | score<60 |

+------+-------+----------+

| 张三     |    90 |        0 |

| 张三     |    50 |        1 |

| 张三     |    40 |        1 |

| 李四     |    55 |        1 |

| 李四     |    45 |        1 |

| 王五     |    30 |        1 |

+------+-------+----------+

6 rows in set (0.00 sec)



mysql> select name,score,sum(score<60) from result group by name;

+------+-------+---------------+

| name | score | sum(score<60) |

+------+-------+---------------+

| 张三     |    90 |             2 |

| 李四     |    55 |             2 |

| 王五     |    30 |             1 |

+------+-------+---------------+

3 rows in set (0.00 sec)



mysql> select name,score,sum(score<60) as g from result group by name having g>=2;

+------+-------+------+

| name | score | g    |

+------+-------+------+

| 张三     |    90 |    2 |

| 李四     |    55 |    2 |

+------+-------+------+

2 rows in set (0.00 sec)



mysql> select name,avg(score) from result group by name having sum(score<60)>=2;

+------+------------+

| name | avg(score) |

+------+------------+

| 张三     |    60.0000 |

| 李四     |    50.0000 |

+------+------------+

2 rows in set (0.00 sec)



mysql> select name,avg(score) from result group by name where sum(score<60)>=2;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where sum(score<60)>=2' at line 1

mysql> select name,avg(score) from result group by name having sum(score<60)>=2;

+------+------------+

| name | avg(score) |

+------+------------+

| 张三     |    60.0000 |

| 李四     |    50.0000 |

+------+------------+

2 rows in set (0.00 sec)



mysql> select name,avg(score),sum(score<60) as g from result group by name having g>=2;

+------+------------+------+

| name | avg(score) | g    |

+------+------------+------+

| 张三     |    60.0000 |    2 |

| 李四     |    50.0000 |    2 |

+------+------------+------+

2 rows in set (0.00 sec)



mysql> #首先将平均数求出,然后再筛选出挂科两科及两科以上的同学,筛选用score<60,是否及格用0,1表示,然后求和就是挂科数,筛选出>=2的,整出结果

mysql> #找出每个栏目下id号最大的商品

mysql> select goods_id,cat_id,goods_name from goods group by cat_id order by goods_id desc limit 1;

+----------+--------+-----------------+

| goods_id | cat_id | goods_name      |

+----------+--------+-----------------+

|       29 |     14 | 移动100元充值卡          |

+----------+--------+-----------------+

1 row in set (0.03 sec)



mysql> select goods_id,cat_id,goods_name from goods group by cat_id having goods_id order by goods_id desc limit 1;

+----------+--------+-----------------+

| goods_id | cat_id | goods_name      |

+----------+--------+-----------------+

|       29 |     14 | 移动100元充值卡          |

+----------+--------+-----------------+

1 row in set (0.00 sec)



mysql> select goods_id,cat_id,goods_name from goods group by cat_id having goods_id order by goods_id desc;

+----------+--------+-----------------------+

| goods_id | cat_id | goods_name            |

+----------+--------+-----------------------+

|       29 |     14 | 移动100元充值卡                |

|       27 |     15 | 联通100元充值卡                |

|       25 |     13 | 小灵通/固话50元充值卡              |

|       23 |      5 | 诺基亚N96                  |

|       16 |      2 | 恒基伟业G101                |

|        8 |      3 | 飞利浦9@9v                  |

|        5 |     11 | 索爱原装M2卡读卡器                  |

|        4 |      8 | 诺基亚N85原装充电器                |

|        1 |      4 | KD876                 |

+----------+--------+-----------------------+

9 rows in set (0.00 sec)



mysql> select goods_id,cat_id,goods_name from goods group by cat_id having max(goods_id);

+----------+--------+-----------------------+

| goods_id | cat_id | goods_name            |

+----------+--------+-----------------------+

|       16 |      2 | 恒基伟业G101                |

|        8 |      3 | 飞利浦9@9v                  |

|        1 |      4 | KD876                 |

|       23 |      5 | 诺基亚N96                  |

|        4 |      8 | 诺基亚N85原装充电器                |

|        5 |     11 | 索爱原装M2卡读卡器                  |

|       25 |     13 | 小灵通/固话50元充值卡              |

|       29 |     14 | 移动100元充值卡                |

|       27 |     15 | 联通100元充值卡                |

+----------+--------+-----------------------+

9 rows in set (0.00 sec)



mysql> select goods_id,cat_id,goods_name from goods group by cat_id where cat_id having max(goods_id);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where cat_id having max(goods_id)' at line 1

mysql> select goods_name,goods_id,cat_id from (select goods_name,cat_id,goods_id from goods order by goods_id desc)as temp group by cat_id;

+------------------------------+----------+--------+

| goods_name                   | goods_id | cat_id |

+------------------------------+----------+--------+

| 恒基伟业G101                       |       16 |      2 |

| 诺基亚N85                         |       32 |      3 |

| 夏新T5                           |       18 |      4 |

| 诺基亚N96                         |       23 |      5 |

| 诺基亚N85原装立体声耳机HS-82                  |        7 |      8 |

| 胜创KINGMAX内存卡                  |        6 |     11 |

| 小灵通/固话20元充值卡                     |       26 |     13 |

| 移动20元充值卡                        |       30 |     14 |

| 联通50元充值卡                        |       28 |     15 |

+------------------------------+----------+--------+

9 rows in set (0.03 sec)



mysql> #必须要用到子查询

 

你可能感兴趣的:(having)