日期:2014-05-18  浏览次数:20547 次

多表联合查询、条件查询再加分页,求指导
表PoolV4: ID,IP,PoolID
表PoolName: ID,Name,HostID
表Host:ID,Name

需求1:多表联合查询,我以如下语句,执行正确
SELECT Poolv4.*,PoolName.Name AS PName,PoolName.ID AS PID, Host.ID AS HID, Host.Name AS HName FROM PoolV4 INNER JOIN PoolName on PoolV4.PoolID=PoolName.ID INNER JOIN Host ON Host.ID=PoolName.HostID

为了加入条件查询,我执行如下语句,执行正确
SELECT * FROM (SELECT Poolv4.*,PoolName.Name AS PName,PoolName.ID AS PID, Host.ID AS HID, Host.Name AS HName FROM PoolV4 INNER JOIN PoolName on PoolV4.PoolID=PoolName.ID INNER JOIN Host ON Host.ID=PoolName.HostID) AS T1 WHERE HID=5

为了对查询结果进行分页,我先进行一第步,也就是对上面语句的结果进行编号,用Row_Number函数,执行正确
SELECT *, Row_Number() OVER (ORDER BY id) AS RowNo FROM ( SELECT * FROM (SELECT Poolv4.*,PoolName.Name AS PName,PoolName.ID AS PID, Host.ID AS HID, Host.Name AS HName FROM PoolV4 INNER JOIN PoolName on PoolV4.PoolID=PoolName.ID INNER JOIN Host ON Host.ID=PoolName.HostID) AS T1 WHERE HID=5) AS T2

但当我进行分面的时候失败了,我先在上面语句后加
WHERE RowNo BETWEEN 1 AND 100
提示RowNo列名无效。

如果我再对第三条语句时行嵌套,语句用 penis代替
SELECT * FROM (PENIS ) WHERE RowNo BETWEEN 1 AND 100
提示WHERE附近语法错误,我想也是因为没能认出RowNo来吧。

求指导。

------解决方案--------------------
最后那个已经很接近了,只是子查询没加别名,呵呵