Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

sql-delete

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

like image 751
balu zapps Avatar asked Apr 16 '13 05:04

balu zapps


People also ask

How can I delete all rows from a certain value in SQL?

To remove one or more rows in a table: First, you specify the table name where you want to remove data in the DELETE FROM clause. Second, you put a condition in the WHERE clause to specify which rows to remove. If you omit the WHERE clause, the statement will remove all rows in the table.

How do I delete multiple rows in a table?

If you want to remove more than one row or column, select a cell in each row or column you want to delete. Under Table Tools, click Layout, and then click either Delete Row or Delete Column.

How do you delete certain rows from a table?

Right-click in a table cell, row, or column you want to delete. On the menu, click Delete Cells. To delete one cell, choose Shift cells left or Shift cells up. To delete the row, click Delete entire row.

Can we delete multiple rows in MySQL?

We can use DELETE statement along with a WHERE clause, which identifies those multiple rows, to delete multiple rows from MySQL table.


2 Answers

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; 
like image 90
Barranka Avatar answered Sep 28 '22 11:09

Barranka


You can use BETWEEN:

DELETE FROM table where id between 163 and 265 
like image 36
leppie Avatar answered Sep 28 '22 11:09

leppie