103

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

我试过这个来删除更少的行数

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

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

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

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

4

6 回答 6

223

如果需要根据列表删除,可以使用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;
于 2013-04-16T05:35:59.937 回答
26

您可以使用BETWEEN

DELETE FROM table
where id between 163 and 265
于 2013-04-16T05:35:27.440 回答
4

请试试这个:

DELETE FROM `table` WHERE id >=163 and id<= 265
于 2016-05-13T04:34:01.487 回答
0
SELECT * FROM your_table ORDER BY DESC

获取要删除的范围 例如:3 到 10

 DELETE FROM your_table
 WHERE id BETWEEN 3 AND 10;

确保在 BETWEEN 子句中添加最小值拳头

于 2021-10-28T10:14:04.927 回答
0

删除4到7个id

DELETE FROM cats
WHERE id BETWEEN 4 AND 7;

按名字

DELETE FROM cats
WHERE name IN ("Lucy","Stella","Max","Tiger");
于 2022-02-04T18:16:44.260 回答
-6
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>
于 2015-10-05T09:00:12.573 回答