Consulta MySQL para excluir alguns dos valores da tabela
Use NOT IN() para excluir alguns valores da tabela.
Vamos primeiro criar uma tabela:
mysql> create table DemoTable791 (
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
FirstName varchar(100)
);
Query OK, 0 rows affected (0.61 sec)
Insira alguns registros na tabela usando o comando insert:
mysql> insert into DemoTable791(FirstName) values('Chris');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable791(FirstName) values('Robert');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable791(FirstName) values('David');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable791(FirstName) values('Mike');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable791(FirstName) values('Bob');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable791(FirstName) values('Carol');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable791(FirstName) values('Adam');
Query OK, 1 row affected (0.09 sec)
Exiba todos os registros da tabela usando a instrução select:
mysql> select *from DemoTable791;
Isso produzirá a seguinte saída -
+----+-----------+
| Id | FirstName |
+----+-----------+
| 1 | Chris |
| 2 | Robert |
| 3 | David |
| 4 | Mike |
| 5 | Bob |
| 6 | Carol |
| 7 | Adam |
+----+-----------+
7 rows in set (0.00 sec)
A seguir está a consulta para excluir alguns dos valores da tabela usando NOT IN() no MySQL:
mysql> select *from DemoTable791 where Id NOT IN(4,5,6);
Isso produzirá a seguinte saída -
+----+-----------+
| Id | FirstName |
+----+-----------+
| 1 | Chris |
| 2 | Robert |
| 3 | David |
| 7 | Adam |
+----+-----------+
4 rows in set (0.00 sec)