好的,我遇到的问题是在下面的查询中选择一个不同的属性。我理解为什么我不能(rownum是唯一的)背后的原因,但我在一个解决方案后,我将保留我的SQL分页,并从我的数据库中只提取不同的属性。我目前正在使用一对多的数据库关系,这些关系也包含结果所需的过滤。我感兴趣的是,如果有比使用" IN"更好的方式。我的过滤。干杯球员
CREATE PROCEDURE [dbo].[getActiveProperties]
@PAGENUM AS INT,
@PERPAGE AS INT,
@SORTBY AS VARCHAR(50),
@FILTERBY AS VARCHAR(200)
AS
DECLARE @query as VARCHAR(2000)
SET @query = N'SELECT *
FROM (SELECT
ROW_NUMBER() OVER (ORDER BY ' + @SORTBY + ') AS [rownum],
dbo.funcSellIdByPropId(T0.id) as SellerId,
dbo.funcDefaultImage(T0.id, 1) as propImage,
dbo.funcDefaultImage(T0.id, 2) as propImage2,
dbo.funcDefaultImage(T0.id, 3) as propImage3,
dbo.funcDefaultImage(T0.id, 4) as propImage4,
dbo.funcCountPropertyImages(T0.id) as imageCount,
dbo.funcGetPropertyTypeListGB(T0.id) as TypeGB,
dbo.funcGetPropertyTypeListFR(T0.id) as TypeFR,
dbo.funcGetPropertyEnviListGB(T0.id) as EnviGB,
dbo.funcGetPropertyEnviListFR(T0.id) as EnviFR,
dbo.funcGetDepartmentByTown(T0.Town) as Department,
dbo.funcCheckFeaturedProperty(T0.id) as Featured,
T0.id, T0.Price, T0.BedRooms, T0.Town, T0.Postcode, T0.Mandate, T0.MinLandArea, T0.Rooms, T0.HabitableSurface, T0.Active, T0.Budget,
T1.TitleFR, T1.TitleGB, SUBSTRING(T1.DescFR, 0, 300) as DescFR, SUBSTRING(T1.DescGB, 0, 300) as DescGB
FROM
PROPERTIES T0
INNER JOIN
PROPERTYTRANSLATIONS T1
ON
T1.PropertyId = T0.id
INNER JOIN
MATRIXPROPENVIRONMENT T2
ON
T2.PropertyId = T0.id
INNER JOIN
ENVIRONMENT T3
ON
T3.id = T2.EnvironmentId
WHERE
T0.Deleted = 0
AND
T0.Active = 1
AND
T3.GB IN (' + @FILTERBY + ')
) T
WHERE
rownum BETWEEN (' + CONVERT(varchar(4), @PERPAGE * @PAGENUM) + ') AND (' + CONVERT(varchar(4), @PERPAGE * (@PAGENUM + 1)) + ')'
EXEC (@query)
答案 0 :(得分:1)
易。将rownum从子查询移动到外部查询:
SET @query = N'SELECT * FROM (
SELECT ROW_NUMBER() OVER (ORDER BY ' + @SORTBY + ') AS [rownum],
*
FROM (SELECT DISTINCT
dbo.funcSellIdByPropId(T0.id) as SellerId,
dbo.funcDefaultImage(T0.id, 1) as propImage,
dbo.funcDefaultImage(T0.id, 2) as propImage2,
dbo.funcDefaultImage(T0.id, 3) as propImage3,
dbo.funcDefaultImage(T0.id, 4) as propImage4,
dbo.funcCountPropertyImages(T0.id) as imageCount,
dbo.funcGetPropertyTypeListGB(T0.id) as TypeGB,
dbo.funcGetPropertyTypeListFR(T0.id) as TypeFR,
dbo.funcGetPropertyEnviListGB(T0.id) as EnviGB,
dbo.funcGetPropertyEnviListFR(T0.id) as EnviFR,
dbo.funcGetDepartmentByTown(T0.Town) as Department,
dbo.funcCheckFeaturedProperty(T0.id) as Featured,
T0.id, T0.Price, T0.BedRooms, T0.Town, T0.Postcode, T0.Mandate, T0.MinLandArea, T0.Rooms, T0.HabitableSurface, T0.Active, T0.Budget,
T1.TitleFR, T1.TitleGB, SUBSTRING(T1.DescFR, 0, 300) as DescFR, SUBSTRING(T1.DescGB, 0, 300) as DescGB
FROM
PROPERTIES T0
INNER JOIN
PROPERTYTRANSLATIONS T1
ON
T1.PropertyId = T0.id
INNER JOIN
MATRIXPROPENVIRONMENT T2
ON
T2.PropertyId = T0.id
INNER JOIN
ENVIRONMENT T3
ON
T3.id = T2.EnvironmentId
WHERE
T0.Deleted = 0
AND
T0.Active = 1
AND
T3.GB IN (' + @FILTERBY + ')
) T ) TT
WHERE
rownum BETWEEN (' + CONVERT(varchar(4), @PERPAGE * @PAGENUM) + ') AND (' + CONVERT(varchar(4), @PERPAGE * (@PAGENUM + 1)) + ')'