如何在SQL中删除多行,其中id =(x到y)

时间:2013-04-16 05:31:15

标签: sql sql-delete

我正在尝试运行SQL查询以删除表中 163到265的行

我试过删除较少的行数

    DELETE FROM `table` WHERE id IN (264, 265)

但是当一次删除100行时,是否有类似于上述方法的查询 我也试图使用这种查询,但未能执行它

    DELETE FROM `table` WHERE id IN (SELECT * FROM table WHERE id = )

请告诉我执行上述操作的查询...

5 个答案:

答案 0 :(得分:139)

如果您需要根据列表进行删除,可以使用IN

delete from your_table
where id in (value1, value2, ...);

如果您需要根据查询结果进行删除,还可以使用IN

delete from your_table
where id in (select aColumn from ...);

(请注意,子查询必须只返回一列)

如果您需要根据一系列值进行删除,请使用BETWEEN或使用不等式:

delete from your_table
where id between bottom_value and top_value;

delete from your_table
where id >= a_value and id <= another_value;

答案 1 :(得分:16)

您可以使用BETWEEN

DELETE FROM table
where id between 163 and 265

答案 2 :(得分:2)

请试试这个:

DELETE FROM `table` WHERE id >=163 and id<= 265

答案 3 :(得分:0)

Delete Id from table where Id in (select id from table)

答案 4 :(得分:-5)

CREATE PROC [dbo].[sp_DELETE_MULTI_ROW]       
@CODE XML
,@ERRFLAG  CHAR(1) = '0' OUTPUT    

AS        

SET NOCOUNT ON  
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED  

DELETE tb_SampleTest
    WHERE 
        CODE IN(
            SELECT Item.value('.', 'VARCHAR(20)')
            FROM  @CODE.nodes('RecordList/ID') AS x(Item)
            )

IF @@ROWCOUNT = 0
    SET @ERRFLAG = 200

SET NOCOUNT OFF

获取字符串值删除

<RecordList>
    <ID>1</ID>
    <ID>2</ID>
</RecordList>