Select all columns except one in MySQL?

后端 未结 30 2887
后悔当初
后悔当初 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:05

    If you use MySQL Workbench you can right-click your table and click Send to sql editor and then Select All Statement This will create an statement where all fields are listed, like this:

    SELECT `purchase_history`.`id`,
        `purchase_history`.`user_id`,
        `purchase_history`.`deleted_at`
    FROM `fs_normal_run_2`.`purchase_history`;
    SELECT * FROM fs_normal_run_2.purchase_history;
    

    Now you can just remove those that you dont want.

    0 讨论(0)
  • 2020-11-22 01:07

    I have a suggestion but not a solution. If some of your columns have a larger data sets then you should try with following

    SELECT *, LEFT(col1, 0) AS col1, LEFT(col2, 0) as col2 FROM table
    
    0 讨论(0)
  • 2020-11-22 01:08

    Just do

    SELECT * FROM table WHERE whatever
    

    Then drop the column in you favourite programming language: php

    while (($data = mysql_fetch_array($result, MYSQL_ASSOC)) !== FALSE) {
       unset($data["id"]);
       foreach ($data as $k => $v) { 
          echo"$v,";
       }      
    }
    
    0 讨论(0)
  • 2020-11-22 01:08

    I would like to add another point of view in order to solve this problem, specially if you have a small number of columns to remove.

    You could use a DB tool like MySQL Workbench in order to generate the select statement for you, so you just have to manually remove those columns for the generated statement and copy it to your SQL script.

    In MySQL Workbench the way to generate it is:

    Right click on the table -> send to Sql Editor -> Select All Statement.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 01:10

    I wanted this too so I created a function instead.

    public function getColsExcept($table,$remove){
        $res =mysql_query("SHOW COLUMNS FROM $table");
    
        while($arr = mysql_fetch_assoc($res)){
            $cols[] = $arr['Field'];
        }
        if(is_array($remove)){
            $newCols = array_diff($cols,$remove);
            return "`".implode("`,`",$newCols)."`";
        }else{
            $length = count($cols);
            for($i=0;$i<$length;$i++){
                if($cols[$i] == $remove)
                    unset($cols[$i]);
            }
            return "`".implode("`,`",$cols)."`";
        }
    }
    

    So how it works is that you enter the table, then a column you don't want or as in an array: array("id","name","whatevercolumn")

    So in select you could use it like this:

    mysql_query("SELECT ".$db->getColsExcept('table',array('id','bigtextcolumn'))." FROM table");
    

    or

    mysql_query("SELECT ".$db->getColsExcept('table','bigtextcolumn')." FROM table");
    
    0 讨论(0)
提交回复
热议问题