要检查字段是否存在,然后返回结果集,可以使用以下语法-
show columns from yourTableName where field='yourColumnName';
让我们首先创建一个表-
mysql> create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserFirstName varchar(20), UserLastName varchar(20), UserAge int, UserAddress varchar(200), UserCountryName varchar(20) );
这是检查字段是否存在然后返回结果集的查询-
mysql> show columns from DemoTable where field='UserCountryName';
这将产生以下输出-
+-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | UserCountryName | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 1 row in set (0.01 sec)
这是列不存在的情况-
mysql>show columns from DemoTable where field='Marks';
这将产生以下输出-
Empty set (0.00 sec)