where和having的区别
| 作用域 | 常见用法 | 使用聚合函数的注意事项 | 其他注意事项 | |
| where | 查询结果返回前对原始数据先进行过滤 | select * from table where xxx | 不能使用聚合函数 |
where后面必须是真实的列名,不能是别名 |
| having | 对sql查询的返回结果进行二次筛选 | select * from table where xxx group by xx having YYY | YYY一般就是聚合函数 | 可以使用别名 |
mysql> select id as IDs,name,sex from test1 having IDs is not null; +------+------+------+ | IDs | name | sex | +------+------+------+ | 1 | hzx | | | 2 | ljh | 男 | | 3 | NULL | 男 | +------+------+------+ 3 rows in set (0.21 sec) mysql> select id as IDs,name,sex from test1 where IDs is not null; ERROR 1054 (42S22): Unknown column 'IDs' in 'where clause' mysql>
浙公网安备 33010602011771号