How to return field type from MySQL query?

后端 未结 5 700
北海茫月
北海茫月 2020-12-09 16:50

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 param

相关标签:
5条回答
  • 2020-12-09 17:13

    You can use

    SHOW FIELDS
    FROM tableName where Field ='nameOfField'
    

    This will return you result in format of

    Field   Type    Null    Key     Default     Extra 
    
    0 讨论(0)
  • 2020-12-09 17:31

    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.

    SELECT COLUMN_TYPE
    FROM information_schema.COLUMNS
    WHERE TABLE_NAME = 'parts'
    AND COLUMN_NAME = 'price'
    

    ...yields...

    decimal(11,2)
    
    0 讨论(0)
  • 2020-12-09 17:31

    The following syntax also works, using describe, enter the columnName after the tableName:

    describe tbleName columnName;
    
    0 讨论(0)
  • 2020-12-09 17:32
    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));
    }
    
    0 讨论(0)
  • 2020-12-09 17:33

    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'
    
    0 讨论(0)
提交回复
热议问题