select a.user_id,user_regTime,count(b.q_id) as num from VIEW_UserInfo as a left join quoted as b on a.user_id=b.q_usernameid and q_sortid=21 group by a.user_id,user_regTime order by user_regtime desc
有这样一个sql语句现在我想把num字段作条件放在where num> 10 里面不知道可行吗? 还有就是我还想把他放在order by num desc里面排序 有什么好办法没啊。
直接把num当做普通字段来写不行啊。求高手指点。。
------解决方案--------------------
having count(b.q_id)> 1
------解决方案--------------------
会影响效率的,建议尽量不要用sql的复合语句
------解决方案--------------------
select a.user_id,user_regTime,count(b.q_id) as num from VIEW_UserInfo as a left join quoted as b on a.user_id=b.q_usernameid and q_sortid=21 group by a.user_id,user_regTime order by user_regtime desc,3 desc
order by 后面跟自然数,表示是以第几项字段进行排序,但无法使用聚合后的别名
聚合后的字段可以直接进行 where 判断,直接使用系统命名或别名都可以
------解决方案--------------------
having