Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return field type from MySQL query?

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.:

SELECT fieldtype(mycol) FROM mytable
# should return INT or integer for example
like image 476
Matt Bannert Avatar asked Feb 23 '11 16:02

Matt Bannert


People also ask

How do I find the data type of a column in a table in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.

How do I return a column in MySQL?

Use the asterisk character (*) in place of a column list in a SELECT statement to instruct MySQL to return every column from the specified table.

How do I SELECT a specific field in MySQL?

If you want to select only specific columns, replace the * with the names of the columns, separated by commas. The following statement selects just the name_id, firstname and lastname fields from the master_name table.


2 Answers

You can use

SHOW FIELDS
FROM tableName where Field ='nameOfField'

This will return you result in format of

Field   Type    Null    Key     Default     Extra 
like image 91
Gaurav Avatar answered Nov 01 '22 14:11

Gaurav


You can get this from the information_schema database:

select data_type 
from information_schema.columns 
where table_schema = 'myschema'
and table_name = 'mytable' 
and column_name = 'mycol'
like image 27
Ike Walker Avatar answered Nov 01 '22 14:11

Ike Walker