在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)
阅读更多:MySQL 教程