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>

 

posted on 2021-10-12 20:51  呼延灼  阅读(72)  评论(0)    收藏  举报

导航