SELECT cols.COLUMN_NAME AS 字段, cols.COLUMN_TYPE AS 数据类型, IF(pks.CONSTRAINT_TYPE = 'PRIMARY KEY', 'YES', 'NO') AS 是否为主键, IF(idxs.INDEX_NAME IS NOT NULL, 'YES', 'NO') AS 是否为索引, cols.IS_NULLABLE AS 是否为空, cols.COLUMN_DEFAULT AS 默认值, cols.COLUMN_COMMENT AS 备注 FROM INFORMATION_SCHEMA.COLUMNS AS cols LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kc ON kc.TABLE_SCHEMA = cols.TABLE_SCHEMA AND kc.TABLE_NAME = cols.TABLE_NAME AND kc.COLUMN_NAME = cols.COLUMN_NAME LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS pks ON pks.TABLE_SCHEMA = kc.TABLE_SCHEMA AND pks.TABLE_NAME = kc.TABLE_NAME AND pks.CONSTRAINT_TYPE = 'PRIMARY KEY' AND kc.CONSTRAINT_NAME = pks.CONSTRAINT_NAME LEFT JOIN INFORMATION_SCHEMA.STATISTICS AS idxs ON idxs.TABLE_SCHEMA = cols.TABLE_SCHEMA AND idxs.TABLE_NAME = cols.TABLE_NAME AND idxs.COLUMN_NAME = cols.COLUMN_NAME WHERE cols.TABLE_SCHEMA = 'db' -- 替换为您的数据库名称 AND cols.TABLE_NAME = 'table' -- 替换为您的表名称 ORDER BY cols.ORDINAL_POSITION ASC; -- 按列在表中的顺序排列