如何从MySQL查询返回字段类型?

Mat*_*ert 18 mysql fieldtype

简单问题:如何返回MySQL表的字段类型.我知道describeshow column但我只是想返回那个单一的参数.例如:

SELECT fieldtype(mycol) FROM mytable
# should return INT or integer for example
Run Code Online (Sandbox Code Playgroud)

Gau*_*rav 21

您可以使用

SHOW FIELDS
FROM tableName where Field ='nameOfField'
Run Code Online (Sandbox Code Playgroud)

这将以格式返回结果

Field   Type    Null    Key     Default     Extra 
Run Code Online (Sandbox Code Playgroud)


Ike*_*ker 17

您可以从information_schema数据库中获取此信息:

select data_type 
from information_schema.columns 
where table_schema = 'myschema'
and table_name = 'mytable' 
and column_name = 'mycol'
Run Code Online (Sandbox Code Playgroud)


Jul*_*les 5

我知道我有点迟到了.您可能希望使用COLUMN_TYPE而不是DATA_TYPE.它提供了有关类型的更多信息(例如精度).

SELECT COLUMN_TYPE
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'parts'
AND COLUMN_NAME = 'price'
Run Code Online (Sandbox Code Playgroud)

... ...产量

decimal(11,2)
Run Code Online (Sandbox Code Playgroud)