检查 MySQL 表列中是否存在某个值?
我们先创建一个表 -
mysql> create table DemoTable807( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100), ClientCountryName varchar(100) ); Query OK, 0 rows affected (0.64 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable807(ClientName,ClientCountryName) values('Chris','UK'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable807(ClientName,ClientCountryName) values('David','AUS'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable807(ClientName,ClientCountryName) values('Robert','US'); Query OK, 1 row affected (0.74 sec) mysql> insert into DemoTable807(ClientName,ClientCountryName) values('Mike','ENG'); Query OK, 1 row affected (0.14 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable807;
这将产生以下输出 -
+----------+------------+-------------------+ | ClientId | ClientName | ClientCountryName | +----------+------------+-------------------+ | 1 | Chris | UK | | 2 | David | AUS | | 3 | Robert | US | | 4 | Mike | ENG | +----------+------------+-------------------+ 4 rows in set (0.00 sec)
以下是检查 MySQL 表列中是否存在某个值所用的查询 -
mysql> select *from DemoTable807 where ClientCountryName='US';
这将产生以下输出 -
+----------+------------+-------------------+ | ClientId | ClientName | ClientCountryName | +----------+------------+-------------------+ | 3 | Robert | US | +----------+------------+-------------------+ 1 row in set (0.00 sec)
广告