我正在尝试在 row_number 中使用变量 order by,当每个案例都有一个定义为没有 asc 或 desc 的列时,它可以工作。但是当我定义多个列或使用 asc 或 desc 时它不起作用。
这有效:
select top(@perpage) *,row_number()
over (order by
case @orderby when 'updated' then date_edited
when 'oldest' then date_added
end
)
as rid from
(select * from ads) a
where rid>=@start and rid<@start+@perpage
但我想这样做:
select top(@perpage) *,row_number()
over (order by
case @orderby when 'updated' then date_edited, user_type desc
when 'oldest' then date_added, user_type desc
when 'newest' then date_added desc, user_type desc
when 'mostexpensive' then price desc, user_type desc
when 'cheapest' then pricedesc, user_type desc
when '' then user_type desc
end
)
as rid from
(select * from ads) a
where rid>=@start and rid<@start+@perpage