SQL选择删除第一行的语句

时间:2015-06-29 09:25:55

标签: sql sql-server sql-server-2008

我在表格中有多行,我使用select语句来获取详细信息。

select * from pdetails where pcode='P000437' 

结果

enter image description here

我的查询打印3条记录,但我不想打印第一行。

是否有任何选项可以使用带有条件的select语句来计算和删除第一行。

5 个答案:

答案 0 :(得分:4)

尝试此查询

 SELECT * FROM (
    select ROW_NUMBER()OVER(ORDER BY id ASC) row,* from pdetails where pcode='P000437'
    )t
    WHERE row != 1;

答案 1 :(得分:3)

您可以使用临时表

来获取此信息
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
DROP TABLE #temp

SELECT *, ROW_NUMBER() over(order by id) as ROWID into #temp FROM pdetails 
WHERE pcode='P000437'

SELECT * FROM #temp WHERE ROWID != 1

答案 2 :(得分:2)

试试这句话

 select * from pdetails where pcode='P000437' 
    order by id
    OFFSET 1 ROWS FETCH NEXT 2 ROWS ONLY

答案 3 :(得分:1)

试试这个:

select * 
from pdetails 
where pcode= 'P000437' AND ID <> (SELECT MIN(ID)
                                 FROM pdetails
                                 WHERE pcode= 'P000437')

答案 4 :(得分:0)

您可以使用ROW_NUMBER()排除第一行(基于Date

像这样。

SELECT * FROM (
select ROW_NUMBER()OVER(ORDER BY Date ASC)rn,* from pdetails where pcode='P000437'
)t
WHERE rn != 1;