120

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...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
balu zapps
  • 1,209
  • 2
  • 9
  • 4
  • 1
    Possible duplicate of [SQL Delete Records within a specific Range](http://stackoverflow.com/questions/8225036/sql-delete-records-within-a-specific-range) –  Dec 14 '16 at 09:42
  • 1
    This is not a duplicated from the above mentioned...the other relates to deletion in range of consecutives, the question in this refers to deletion of possible non-consecutive id's, a list of ids.. – Merak Marey Sep 01 '19 at 07:35

5 Answers5

247

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;
Community
  • 1
  • 1
Barranka
  • 20,547
  • 13
  • 65
  • 83
32

You can use BETWEEN:

DELETE FROM table
where id between 163 and 265
leppie
  • 115,091
  • 17
  • 196
  • 297
6

Please try this:

DELETE FROM `table` WHERE id >=163 and id<= 265
Steven
  • 1,996
  • 3
  • 22
  • 33
Keerthi
  • 525
  • 8
  • 14
0
SELECT * FROM your_table ORDER BY DESC

Get the range that you want to delete Ex: 3 to 10

 DELETE FROM your_table
 WHERE id BETWEEN 3 AND 10;

Make sure to add min value fist in BETWEEN Clause

-7
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>
slavoo
  • 5,798
  • 64
  • 37
  • 39
ThienPhuc
  • 1
  • 3