Delete multiple rows from a database

Asked

Viewed 4,763 times

7

I’d like to delete several figures. For example this would be to delete a line

  delete from paginasimagens where idPaginasImagens=121;

my values are sequentially so a method that is between elements serves;

  • 2

    you can use a where coluna in (1,2,3) or where coluna between 1 and 30

  • There are some techniques described in Delete row set in huge tables -> https://portosql.wordpress.com/2019/10/16/delete-vlt/

4 answers

11

If you will manually type the Ids:

delete from paginasimagens where idPaginasImagens in (121, 122, 123)

If Ids come from a query:

delete from paginasimagens where idPaginasImagens in (SELECT idPaginasImagens FROM outraTabela)

If you want to delete a sequence of Ids:

delete from paginasimagens where idPaginasImagens between 1 and 30
  • 2

    In my opinion, this is the most appropriate answer to the user’s doubt.

7


Use the operator BETWEEN to obtain the desired result, using the first value (which will be included in the exclusion) and the last value (which will also be deleted):

DELETE FROM paginasimagens
 WHERE idPaginasImagens BETWEEN 121 AND 123;

BETWEEN

If expr is Greater than or Equal to min and expr is Less than or Equal to max, BETWEEN Returns 1, otherwise it Returns 0.

In free translation:

If expr is greater than or equal to the minimum and expr is less than or equal to the maximum, BETWEEN return 1, or return 0.

1

To delete all table data also, with TRUNCATE:

TRUNCATE TABLE paginasimagens;
  • 3

    Even if formatting the server usually erases the lines together

  • 4

    @Sorack hardware alternative https://imgur.com/j0MqK4e

  • 2

    @Bacco justo hahaha

-6

Utilizes the checkbox in your table name="item[]" in PHP creates a loop with foreach:

foreach($_POST['item'] as $item) {

  $sql("delete from paginasimagens WHERE id = '$item'");<br>
  $sql = $conexao->query($sql) or die($conexao->error);

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.