where 与 having 区别
区别:where 筛选的是数据库表里原本就有的字段,而 having 筛选的是前面需要查询的字段。
(1)where 和 having 都能用:
select sessionid, starttime from wc_chat_sessionlist where starttime > '2022-04-01 09:56:12'; select sessionid, starttime from wc_chat_sessionlist having starttime > '2022-04-01 09:56:12';
(2)只可以使用where,不可以使用 having 的情况:
select sessionid, starttime from wc_chat_sessionlist where endtime > '2022-04-01 09:56:12'; select sessionid, starttime from wc_chat_sessionlist having endtime > '2022-04-01 09:56:12'; -- 会报错
使用 having 的报错信息:
(3)只可以使用 having,不可以使用 where 的情况:
select serviceid, count(sessionid) as serviceNum from wc_chat_sessionlist group by serviceid ; select serviceid, count(sessionid) as serviceNum from wc_chat_sessionlist where serviceNum > 100 group by serviceid ; -- 会报错 select serviceid, count(sessionid) as serviceNum from wc_chat_sessionlist group by serviceid having serviceNum > 100;
原本分组数据:
使用 where 的报错信息:
使用 having 的数据:
分类:
数据库
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?