且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL查询以获取列名?

更新时间:2022-11-08 10:23:29

***的方法是使用 INFORMATION_SCHEMA 元数据虚拟数据库.特别是 INFORMATION_SCHEMA.COLUMNS 表...

The best way is to use the INFORMATION_SCHEMA metadata virtual database. Specifically the INFORMATION_SCHEMA.COLUMNS table...

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA`='yourdatabasename' 
    AND `TABLE_NAME`='yourtablename';

它非常强大,无需解析文本即可为您提供大量信息(例如列类型、列是否可为空、最大列大小、字符集等)...

It's VERY powerful, and can give you TONS of information without need to parse text (Such as column type, whether the column is nullable, max column size, character set, etc)...

哦,它是标准 SQL(而 SHOW ... 是 MySQL 特定的扩展)...

Oh, and it's standard SQL (Whereas SHOW ... is a MySQL specific extension)...

有关 SHOW... 和使用 INFORMATION_SCHEMA 表之间区别的更多信息,请查看 MySQL 关于INFORMATION_SCHEMA 的一般文档...

For more information about the difference between SHOW... and using the INFORMATION_SCHEMA tables, check out the MySQL Documentation on INFORMATION_SCHEMA in general...