聚合不应出现在 WHERE 子句中
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
--资讯新闻百万级分页
(
@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
...全文
相关推荐
Hive中查询操作
- `WHERE` 子句用来设置查询条件,只有满足条件的行才会被返回。- `GROUP BY` 用于对数据进行分组,通常与聚合函数(如COUNT, SUM, AVG等)一起使用。
学习sql总结
**WHERE子句的重要性**:在构建SQL查询时,WHERE子句的编写非常关键。一个更具体的WHERE条件不仅可以减少数据扫描量,还能提高查询效率。
常用的SQL开发技巧
**利用WHERE子句限制查询结果集**:通过WHERE子句过滤不必要的数据,可以让SQL Server仅返回特定行,减少网络传输的数据量,从而提升查询性能。2.
Oracle数据库应用教程子查询与高级查询.pptx
需要注意的是,子查询不应包含ORDER BY子句,因为排序只在最终结果集上进行。3. **在HAVING子句中使用子查询**:HAVING子句常用于带有聚合函数的查询,它允许在分组后对结果进行过滤。
常用的hql语句用法解析
复杂的查询可以选择多个对象或属性,或者使用聚合函数,如`select count(*) from Cat`计算猫的数量。5. `where`子句`where`子句用于添加过滤条件。


疑难问题
