如何从 MySQL 查询返回字段类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5093551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return field type from MySQL query?
提问by Matt Bannert
Simple question: How can I return the field type of a MySQL table. I know about describe
or show column
but I just want to return that single parameter. e.g.:
简单的问题:如何返回 MySQL 表的字段类型。我知道describe
orshow column
但我只想返回那个单个参数。例如:
SELECT fieldtype(mycol) FROM mytable
# should return INT or integer for example
回答by Gaurav
You can use
您可以使用
SHOW FIELDS
FROM tableName where Field ='nameOfField'
This will return you result in format of
这将以以下格式返回结果
Field Type Null Key Default Extra
回答by Ike Walker
You can get this from the information_schema database:
您可以从 information_schema 数据库中获取:
select data_type
from information_schema.columns
where table_schema = 'myschema'
and table_name = 'mytable'
and column_name = 'mycol'
回答by Jules
I know I'm a little late to the party. You may want to use COLUMN_TYPE rather than DATA_TYPE. It gives more info (such as precision) about the type.
我知道我参加聚会有点晚了。您可能希望使用 COLUMN_TYPE 而不是 DATA_TYPE。它提供了有关该类型的更多信息(例如精度)。
SELECT COLUMN_TYPE
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'parts'
AND COLUMN_NAME = 'price'
...yields...
...收益...
decimal(11,2)
回答by spelunk1
The following syntax also works, using describe, enter the columnName after the tableName:
以下语法也适用,使用 describe,在 tableName 后输入 columnName:
describe tbleName columnName;
回答by SRK
ResultSet rs = Sstatement.executeQuery("SELECT * FROM Table Name");
ResultSetMetaData rsMetaData = rs.getMetaData();
int numberOfColumns = rsMetaData.getColumnCount();
System.out.println("resultSet MetaData column Count=" + numberOfColumns);
for (int i = 1; i <= numberOfColumns; i++) {
System.out.println("column number " + i);
System.out.println(rsMetaData.getColumnTypeName(i));
}