如何在 MySQL 中排除具体记录?
可以使用不等于运算符(!=)在 SQL 中排除具体记录。我们首先创建一个表−
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(20), ClientCountryName varchar(10) ); Query OK, 0 rows affected (0.64 sec)
使用 insert 命令在表中插入记录−
mysql> insert into DemoTable(ClientName,ClientCountryName) values('John','US'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(ClientName,ClientCountryName) values('David','AUS'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(ClientName,ClientCountryName) values('Mike','UK'); Query OK, 1 row affected (0.14 sec)
使用 select 语句显示表中的所有记录−
mysql> select *from DemoTable;
这将生成以下输出−
+----+------------+-------------------+ | Id | ClientName | ClientCountryName | +----+------------+-------------------+ | 1 | John | US | | 2 | David | AUS | | 3 | Mike | UK | +----+------------+-------------------+ 3 rows in set (0.00 sec)
以下是排除 MySQL 中具体记录(即排除 ClientName 为 David' 或 ClientCountryName = 'AUS' 的记录)的查询−
mysql> select *from DemoTable where ClientName!='David' or ClientCountryName!='AUS';
这将生成以下输出−
+----+------------+-------------------+ | Id | ClientName | ClientCountryName | +----+------------+-------------------+ | 1 | John | US | | 3 | Mike | UK | +----+------------+-------------------+ 2 rows in set (0.00 sec)
广告