MySQL Select 在一列中显示数据类型?
你可以为此使用 INFORMATION_SCHEMA.COLUMNS。我们首先创建一个表 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) ); Query OK, 0 rows affected (0.73 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.12 sec)
使用 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)
广告