MySQL query to get column names?

前端 未结 21 2053
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 01:56

I\'d like to get all of a mysql table\'s col names into an array in php?

Is there a query for this?

相关标签:
21条回答
  • 2020-11-22 02:32

    I have done this in the past.

    SELECT column_name
    FROM information_schema.columns
    WHERE table_name='insert table name here'; 
    
    0 讨论(0)
  • 2020-11-22 02:34

    if you only need the field names and types (perhaps for easy copy-pasting into Excel):

    SELECT COLUMN_NAME, DATA_TYPE
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA='databasenamegoeshere'
    AND DATA_TYPE='decimal' and TABLE_NAME = 'tablenamegoeshere'
    

    remove

    DATA_TYPE='decimal'
    

    if you want all data types

    0 讨论(0)
  • 2020-11-22 02:36

    when you want to check your all table structure with some filed then use this code. In this query i select column_name,column_type and table_name for more details . I use order by column_type so i can see it easily.

    SELECT `COLUMN_NAME`,COLUMN_TYPE,TABLE_NAME 
    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
    WHERE `TABLE_SCHEMA`='yourdatabasename' order by DATA_TYPE;
    

    If you want to check only double type filed then you can do it easily

    SELECT `COLUMN_NAME`,COLUMN_TYPE,TABLE_NAME,DATA_TYPE 
    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
    WHERE `TABLE_SCHEMA`='yourdatabasename' AND DATA_TYPE like '%bigint%'  order by DATA_TYPE;
    

    if you want to check which field allow null type etc then you can use this

    SELECT `COLUMN_NAME`,COLUMN_TYPE,TABLE_NAME,IS_NULLABLE,DATA_TYPE 
    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
    WHERE `TABLE_SCHEMA`='yourdatabasename' and DATA_TYPE like '%bigint%' and IS_NULLABLE ='NO' order by COLUMN_TYPE;
    

    you want to check more then thik link also help you.

    https://dev.mysql.com/doc/refman/5.7/en/columns-table.html

    0 讨论(0)
  • 2020-11-22 02:38

    Seems there are 2 ways:

    DESCRIBE `tablename`
    

    or

    SHOW COLUMNS FROM `tablename`
    

    More on DESCRIBE here: http://dev.mysql.com/doc/refman/5.0/en/describe.html

    0 讨论(0)
  • 2020-11-22 02:40

    You can use following query for MYSQL:

    SHOW columns FROM your-table;
    

    Below is the example code which shows How to implement above syntax in php to list the names of columns:

    $sql = "SHOW COLUMNS FROM your-table";
    $result = mysqli_query($conn,$sql);
    while($row = mysqli_fetch_array($result)){
        echo $row['Field']."<br>";
    }
    

    For Details about output of SHOW COLUMNS FROM TABLE visit: MySQL Refrence.

    0 讨论(0)
  • 2020-11-22 02:40

    Try this one out I personally use it:

    SHOW COLUMNS FROM $table where field REGEXP 'stock_id|drug_name' 
    
    0 讨论(0)
提交回复
热议问题