您可以为此使用INFORMATION_SCHEMA.COLUMNS。以下是语法-
SELECT COLUMN_NAME,COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='yourTableName';
让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(60), ClientAge int, ClientSalary DECIMAL(10,4), isRegularClient bool );
以下是从SQL数据库获取字段名称类型的查询-
mysql> SELECT COLUMN_NAME,COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='DemoTable';
这将产生以下输出-
+-----------------+---------------+ | COLUMN_NAME | COLUMN_TYPE | +-----------------+---------------+ | Id | int(11) | | ClientName | varchar(60) | | ClientAge | int(11) | | ClientSalary | decimal(10,4) | | isRegularClient | tinyint(1) | +-----------------+---------------+ 5 rows in set (0.02 sec)