日期:2014-05-17  浏览次数:20360 次

这条SQL语句怎么写才对啊?
select *,Row_Number() over(order by id)as Row
from
(
 select id,title,[content] from t_news where title like '%童丽%'
 union
 select id,title,[content] from t_news where [content] like '%童丽%'
)t
where Row between 3 and 5



列名 'Row' 无效。

是不是因为表t中没有Row列的原因?
该怎么改呢?

------解决方案--------------------
SELECT * FROM
(
select *,Row_Number() over(order by id)as Rowid
from
(
select id,title,[content] from t_news where title like '%童丽%'
union
select id,title,[content] from t_news where [content] like '%童丽%'
) t
) m
where Rowid between 3 and 5

------解决方案--------------------
--你说的对。是因为表t中没有Row列的原因。参考如下(select的步骤是8,where是4,会在select之前执行,这时where中是看不到row这个列的):
--SQL逻辑执行顺序
(8)    SELECT (9) DISTINCT (11) <TOP_specification> <select_list>
(1)    FROM <left_table>
(3)        <join_type> JOIN <right_table>
(2)            ON <join_condition>
(4)    WHERE <where_condition>
(5)    GROUP BY <group_by_list>
(6)    WITH {CUBE 
------解决方案--------------------
 ROLLUP}    
(7)    HAVING <having_condition>
(10)ORDER BY <order_by_list>
------解决方案--------------------
简化

select *
from
(select id,title,[content],Row_Number() over(order by id)as Row 
from t_news 
where title like '%童丽%' or [content] like '%童丽%'
)t
where Row between 3 and 5