8

如何从表中返回除第一行之外的所有行。这是我的sql语句:

Select Top(@TopWhat) * 
from tbl_SongsPlayed 
where Station = @Station 
order by DateTimePlayed DESC

如何更改我的 SQL 语句以返回除第一行之外的所有行。

非常感谢

4

4 回答 4

31

SQL 2012 也有相当方便的 OFFSET 子句:

Select Top(@TopWhat) *
from tbl_SongsPlayed 
where Station = @Station 
order by DateTimePlayed DESC
OFFSET 1 ROWS
于 2013-02-22T20:45:20.477 回答
6

根据您的数据库产品,您可以使用row_number()

select *
from
(
  Select s.*,
    row_number() over(order by DateTimePlayed DESC) rn
  from tbl_SongsPlayed s
  where s.Station = @Station 
) src
where rn >1
于 2013-02-22T20:41:43.850 回答
1

'Chrisb' 已经给出了一个非常简洁的答案。不过你也可以试试这个...

EXCEPT 操作数 ( http://msdn.microsoft.com/en-us/library/ms188055.aspx )

Select Top(@TopWhat) *
from tbl_SongsPlayed 
Except  Select Top(1) *
from tbl_SongsPlayed 
where Station = @Station 
order by DateTimePlayed DESC

'Not In' 是另一个可以使用的子句。

于 2013-02-22T21:28:14.590 回答
0

假设您有一个唯一的 ID tbl_SongsPlayed,您可以执行以下操作:

// Filter the songs first
With SongsForStation
As   (
   Select *
   From   tbl_SongsPlayed
   Where  Station = @Station
)
// Get the songs
Select *
From   SongsForStation
Where  SongPlayId <> (
   // Get the top song, most recently played, so you can exclude it.
   Select Top 1 SongPlayId
   From   SongsForStation
   Order By DateTimePlayed Desc
   )
// Sort the rest of the songs.
Order By
   DateTimePlayed desc
        Where 
于 2013-02-22T20:45:04.377 回答