您可以为此使用INFORMATION_SCHEMA.COLUMNS。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(Name) values('Chris'); mysql> insert into DemoTable(Name) values('Robert'); mysql> insert into DemoTable(Name) values('Sam');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+--------+ | Id | Name | +----+--------+ | 1 | Chris | | 2 | Robert | | 3 | Sam | +----+--------+ 3 rows in set (0.00 sec)
以下是显示数据类型和数据值的查询-
mysql> SELECT DemoTable.Name, col1.DATA_TYPE FROM DemoTable,INFORMATION_SCHEMA.COLUMNS col1 WHERE col1.TABLE_NAME='DemoTable' AND COLUMN_NAME='Name';
这将产生以下输出-
+--------+-----------+ | Name | DATA_TYPE | +--------+-----------+ | Chris | varchar | | Robert | varchar | | Sam | varchar | +--------+-----------+ 3 rows in set (0.04 sec)