我在 PHP 环境中使用 SQL Server 和 Datatables;我研究了一下,发现通过使用 row_number 我可以实现分页结果,但我得到:
无法绑定多部分标识符“Flags.FlagValue”。
SELECT dbo.PlayingCharacters.PlayerName,dbo.PlayingCharacters.CurrentLevel,dbo.PlayingCharacters.XP,(SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma,ISNULL(Flags.FlagValue,0) AS Remort FROM ( SELECT row_number() OVER (ORDER BY dbo.PlayingCharacters.XP desc) AS CI_offset_row_number, dbo.PlayingCharacters.PlayerName, dbo.PlayingCharacters.CurrentLevel, dbo.PlayingCharacters.XP, (SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma, ISNULL(Flags.FlagValue, 0) AS Remort FROM dbo.PlayingCharacters LEFT JOIN dbo.Flags ON dbo.Flags.OwnerID = dbo.PlayingCharacters.UserID AND Flags.FlagID = 30419 WHERE dbo.PlayingCharacters.AccountName NOT IN (SELECT DISTINCT AccountName FROM UserFlags WHERE FlagBitPosition BETWEEN 0 AND 40) AND PlayingCharacters.AccountName NOT LIKE 'DeletedFrom:%' ) AS A WHERE A.CI_offset_row_number BETWEEN (141) AND (150)
在检查查询和进一步研究后,我发现查询末尾附近的“AS A”可能是罪魁祸首。但我不确定。我在一定程度上熟悉 SQL,但这样看来我是一条出水的鱼。我似乎无法弄清楚如何修复这个查询。
如果之前可能有人问过这个问题,我深表歉意,我发现了一些关于我的错误的结果,但无法将任何成功的答案组合在一起。
作为旁注,这是我在尝试添加限制/偏移之前的查询row_number()
SELECT TOP 30 dbo.PlayingCharacters.PlayerName, dbo.PlayingCharacters.CurrentLevel, dbo.PlayingCharacters.XP, (SELECT CASE WHEN dbo.PlayingCharacters.Karma >= 1 THEN 2 WHEN dbo.PlayingCharacters.Karma <= -1 THEN 1 ELSE 0 END) as Karma, ISNULL(Flags.FlagValue, 0) AS Remort
FROM dbo.PlayingCharacters
LEFT JOIN dbo.Flags ON dbo.Flags.OwnerID = dbo.PlayingCharacters.UserID AND Flags.FlagID = 30419
WHERE dbo.PlayingCharacters.AccountName NOT IN (SELECT DISTINCT AccountName FROM UserFlags WHERE FlagBitPosition BETWEEN 0 AND 40) AND PlayingCharacters.AccountName NOT LIKE 'DeletedFrom:%'
ORDER BY dbo.PlayingCharacters.XP desc
哪个有效,但显然没有执行所需的限制/偏移。我用来生成相关查询的代码来自:http ://codeigniter.com/forums/viewthread/160626/P10/#985759