关键词搜索

源码搜索 ×
×

mysql where和having的区别

发布2019-04-27浏览3425次

详情内容

       先来看表:

  1. mysql> show create table tbxxx;
  2. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | Table | Create Table |
  4. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | tbxxx | CREATE TABLE `tbxxx` (
  6. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  7. `name` varchar(128) NOT NULL DEFAULT '',
  8. `type` int(10) unsigned NOT NULL DEFAULT '0',
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 |
  11. +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  12. 1 row in set (0.00 sec)
  13. mysql>

        表中数据:

  1. mysql> select * from tbxxx;
  2. +----+------+------+
  3. | id | name | type |
  4. +----+------+------+
  5. | 1 | n1 | 1 |
  6. | 2 | n1 | 2 |
  7. | 3 | n1 | 3 |
  8. | 4 | n2 | 2 |
  9. | 5 | n3 | 3 |
  10. | 6 | n4 | 4 |
  11. +----+------+------+
  12. 6 rows in set (0.00 sec)

         

        下面来看看语句:

  1. mysql> select name from tbxxx where name = 'n1';
  2. +------+
  3. | name |
  4. +------+
  5. | n1 |
  6. | n1 |
  7. | n1 |
  8. +------+
  9. 3 rows in set (0.00 sec)
  10. mysql> select name from tbxxx having name = 'n1';
  11. +------+
  12. | name |
  13. +------+
  14. | n1 |
  15. | n1 |
  16. | n1 |
  17. +------+
  18. 3 rows in set (0.00 sec)

          再看:

  1. mysql> select name from tbxxx where id = 1;
  2. +------+
  3. | name |
  4. +------+
  5. | n1 |
  6. +------+
  7. 1 row in set (0.00 sec)
  8. mysql> select name from tbxxx having id = 1;
  9. ERROR 1054 (42S22): Unknown column 'id' in 'having clause'
  10. mysql>

         再看:

  1. mysql> select name as x from tbxxx where x = 'n1';
  2. ERROR 1054 (42S22): Unknown column 'x' in 'where clause'
  3. mysql> select name as x from tbxxx having x = 'n1';
  4. +----+
  5. | x |
  6. +----+
  7. | n1 |
  8. | n1 |
  9. | n1 |
  10. +----+
  11. 3 rows in set (0.00 sec)

         再看:

  1. mysql> select name , count(name) as x from tbxxx group by name;
  2. +------+---+
  3. | name | x |
  4. +------+---+
  5. | n1 | 3 |
  6. | n2 | 1 |
  7. | n3 | 1 |
  8. | n4 | 1 |
  9. +------+---+
  10. 4 rows in set (0.00 sec)
  11. mysql>
  12. mysql> select name , count(name) as x from tbxxx group by name where x = 3;
  13. 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 x = 3' at line 1
  14. mysql> select name , count(name) as x from tbxxx group by name having x = 3;
  15. +------+---+
  16. | name | x |
  17. +------+---+
  18. | n1 | 3 |
  19. +------+---+
  20. 1 row in set (0.00 sec)

          再看:

  1. mysql> select name , count(name) as x from tbxxx where name != 'n3' group by name having x > 0;
  2. +------+---+
  3. | name | x |
  4. +------+---+
  5. | n1 | 3 |
  6. | n2 | 1 |
  7. | n4 | 1 |
  8. +------+---+
  9. 3 rows in set (0.00 sec)

       小结一下:

       1.  按照这种顺序去写: where > group > having

       2.  用where的时,必须在原始表中有对应的字段名(不要求结果中有), 用having的时候,必须是结果中有对应的字段名(不要求原始表中有)

 

      遇到了,所以记录一下。

      不多说。

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载