我要如何使用 `SHOW COLUMNS` 作为表格的有效数据来源?


为此,您可以使用 INFORMATION_SCHEMA.COLUMNS,如下面的语法所示 −

SELECT *FROM (SELECT *FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME= 'yourTableName')
anyAliasName;

我们首先创建一个表格

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentFirstName varchar(20),
   StudentLastName varchar(20),
   StudentAge int
);
Query OK, 0 rows affected (1.51 sec)

以下是对 `SHOW COLUMNS` 作为有效数据源的用法查询 −

mysql> SELECT *FROM (SELECT *FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME= 'DemoTable')tbl;

这将产生以下输出 −

+---------------+--------------+-------------+------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+-----------------+-------------+------------+----------------+---------------------------------+----------------+-----------------------+--------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME       | ORDINAL_POSITION | COLUMN_DEFAULT | IS_NULLABLE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE | DATETIME_PRECISION | CHARACTER_SET_NAME | COLLATION_NAME | COLUMN_TYPE | COLUMN_KEY | EXTRA | PRIVILEGES | COLUMN_COMMENT | GENERATION_EXPRESSION | SRS_ID |
+---------------+--------------+-------------+------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+-----------------+-------------+------------+----------------+---------------------------------+----------------+-----------------------+--------+
| def           | sample       | DemoTable   | StudentId        | 1                | NULL           | NO          | int       | NULL                     | NULL                   | 10                 | 0 | NULL | NULL | NULL | int(11) | PRI | auto_increment | select,insert,update,references | | | NULL |
| def           | sample       | DemoTable   | StudentFirstName | 2                | NULL           | YES         | varchar   | 20                       | 60                     | NULL               | NULL | NULL | utf8 | utf8_general_ci | varchar(20) | | | select,insert,update,references | | | NULL |
| def           | sample       | DemoTable   | StudentLastName  | 3                | NULL           | YES         | varchar   | 20                       | 60                     | NULL               | NULL | NULL | utf8 | utf8_general_ci | varchar(20) | | | select,insert,update,references | | | NULL |
| def           | sample       | DemoTable   | StudentAge       | 4                | NULL           | YES         | int       | NULL                     | NULL                   | 10                 | 0 | NULL | NULL | NULL | int(11) | | | select,insert,update,references | | | NULL |
+---------------+--------------+-------------+------------------+------------------+----------------+-------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+-----------------+-------------+------------+----------------+---------------------------------+----------------+-----------------------+--------+
4 rows in set (0.00 sec)

更新于: 2019 年 7 月 30 日

75 次浏览

开始您的 职业

通过完成课程获得认证

开始
广告