Select all columns except one in MySQL?

后端 未结 30 2856
后悔当初
后悔当初 2020-11-22 00:46

I\'m trying to use a select statement to get all of the columns from a certain MySQL table except one. Is there a simple way to do this?

EDIT: There are 53 columns i

30条回答
  •  被撕碎了的回忆
    2020-11-22 01:09

    You can use SQL to generate SQL if you like and evaluate the SQL it produces. This is a general solution as it extracts the column names from the information schema. Here is an example from the Unix command line.

    Substituting

    • MYSQL with your mysql command
    • TABLE with the table name
    • EXCLUDEDFIELD with excluded field name
    echo $(echo 'select concat("select ", group_concat(column_name) , " from TABLE") from information_schema.columns where table_name="TABLE" and column_name != "EXCLUDEDFIELD" group by "t"' | MYSQL | tail -n 1) | MYSQL
    

    You will really only need to extract the column names in this way only once to construct the column list excluded that column, and then just use the query you have constructed.

    So something like:

    column_list=$(echo 'select group_concat(column_name) from information_schema.columns where table_name="TABLE" and column_name != "EXCLUDEDFIELD" group by "t"' | MYSQL | tail -n 1)
    

    Now you can reuse the $column_list string in queries you construct.

提交回复
热议问题