聚合不应出现在 WHERE 子句中

tongchou tongchou 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
CREATE procedure T_list

--资讯新闻百万级分页
(
@StrWhere varchar(100),
@PageSize int,
@PageIndex int
)

AS

declare @strSQL varchar(2000) -- 主语句
declare @strCountSQL varchar(2000) -- 总记录主语句
declare @strTmp varchar(1000) -- 临时变量

Set @strTmp =" Select top " + str(@PageSize) + " yixiang_productshow.id,yixiang_productshow.cpmc,yixiang_productshow.cpcd,yixiang_productshow.cpjg,yixiang_productshow.picture,gsid,yixiang_productshow.cpxh,yixiang_corporation.qymc,yixiang_corporation.name,yixiang_corporation.phone FROM yixiang_productshow INNER JOIN yixiang_corporation ON yixiang_productshow.gsid=yixiang_corporation.id "

if @StrWhere<>''
Begin

Set @strSQL=@strTmp + " where ID < (select min(ID) from (select top " + str((@PageIndex-1)*@PageSize)+" ID from yixiang_productshow Where "+@StrWhere+" order by ID desc) as tblTmp ) and "+@StrWhere+" order by ID desc"




聚合不应出现在 WHERE 子句中,除非该聚合位于 HAVING 子句或选择列表所包含的子查询中,并且要对其进行聚合的列是外部引用。


print @strSQL

set @strCountSQL="select count(yixiang_productshow.ID) as countx from yixiang_productshow INNER JOIN yixiang_corporation ON yixiang_productshow.gsid=yixiang_corporation.id Where "+@StrWhere+" "


End

else
Begin
Set @strSQL=@strTmp + " where yixiang_productshow.ID < (select min(yixiang_productshow.ID) from (select top " + str((@PageIndex-1)*@PageSize)+" yixiang_productshow.ID from yixiang_productshow INNER JOIN yixiang_corporation ON yixiang_productshow.gsid=yixiang_corporation.id order by yixiang_productshow.ID desc) as tblTmp ) order by yixiang_productshow.ID desc"
set @strCountSQL="select count(yixiang_productshow.ID) as countx from yixiang_productshow INNER JOIN yixiang_corporation ON yixiang_productshow.gsid=yixiang_corporation.id "

End






if @PageIndex = 1
if @StrWhere<>''
Begin

Set @strSQL=@strTmp +" Where "+@StrWhere+" order by yixiang_productshow .ID desc"

End
else
Begin

Set @strSQL=@strTmp +" order by yixiang_productshow .ID desc"

End

exec (@strSQL)




exec (@strCountSQL)
GO
...全文
评论 6
收藏 已收藏
分享
返回顶部
tongchou
疑难问题 22,298 成员 121,727 内容