How to delete multiple rows in SQL where id = (x t

2019-01-13 00:14发布

I am trying to run a SQL query to delete rows with id's 163 to 265 in a table

I tried this to delete less number of rows

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

But when it comes to delete 100's of rows at a time, Is there any query similar to above method I am also trying to use this kind of query but failed to execute it

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

Please tell me the query to do the above action...

5条回答
姐就是有狂的资本
2楼-- · 2019-01-13 00:23
Delete Id from table where Id in (select id from table)
查看更多
够拽才男人
3楼-- · 2019-01-13 00:29

Please try this:

DELETE FROM `table` WHERE id >=163 and id<= 265
查看更多
做个烂人
4楼-- · 2019-01-13 00:38

If you need to delete based on a list, you can use IN:

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

If you need to delete based on the result of a query, you can also use IN:

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

(Notice that the subquery must return only one column)

If you need to delete based on a range of values, either you use BETWEEN or you use inequalities:

delete from your_table
where id between bottom_value and top_value;

or

delete from your_table
where id >= a_value and id <= another_value;
查看更多
\"骚年 ilove
5楼-- · 2019-01-13 00:43

You can use BETWEEN:

DELETE FROM table
where id between 163 and 265
查看更多
ら.Afraid
6楼-- · 2019-01-13 00:44
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

Get string value delete

<RecordList>
    <ID>1</ID>
    <ID>2</ID>
</RecordList>
查看更多
登录 后发表回答