Mysql中where和having(筛选)的区别:
下面是mysql中的select查询语句:
mysql> select sname,(age-sno) as a from student;
+--------+------+
| sname | a |
+--------+------+
| 换换 | 22 |
| 刘丽 | 20 |
| NULL | 6 |
| 张友 | 17 |
+--------+------+
4 rows in set (0.00 sec)
mysql> select sname,(age-sno) as a from student where a > 10;
ERROR 1054 (42S22): Unknown column 'a' in 'where clause'
注意:编程中任何的查询结果都可以作为变量使用,mysql中也通用.
当使用where条件匹配是报错,区别:
where查询针对的是磁盘,“select sname,(age-sno) as a from student where a > 10;”
where之前的select查询结果也就是“a”的值会临时存放在内存中,后续的where条件匹配时磁盘数据库中没有“a”列,匹配不到就会报错.
使用having查询过滤匹配:
mysql> select sname,(age-sno) as a from student having a > 10;
+--------+------+
| sname | a |
+--------+------+
| 换换 | 22 |
| 刘丽 | 20 |
| 张友 | 17 |
+--------+------+
3 rows in set (0.00 sec)
注意:having的条件匹配是针对内存中的结果集.
转载于:https://blog.51cto.com/215687833/2333518