Select all columns except one in MySQL?

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

    I agree that it isn't sufficient to Select *, if that one you don't need, as mentioned elsewhere, is a BLOB, you don't want to have that overhead creep in.

    I would create a view with the required data, then you can Select * in comfort --if the database software supports them. Else, put the huge data in another table.

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

    I agree with @Mahomedalid's answer, but I didn't want to do something like a prepared statement and I didn't want to type all the fields, so what I had was a silly solution.

    Go to the table in phpmyadmin->sql->select, it dumps the query: copy, replace and done! :)

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

    While I agree with Thomas' answer (+1 ;)), I'd like to add the caveat that I'll assume the column that you don't want contains hardly any data. If it contains enormous amounts of text, xml or binary blobs, then take the time to select each column individually. Your performance will suffer otherwise. Cheers!

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

    Im pretty late at throing out an answer for this, put this is the way i have always done it and frankly, its 100 times better and neater than the best answer, i only hope someone will see it. And find it useful

        //create an array, we will call it here. 
        $here = array();
        //create an SQL query in order to get all of the column names
        $SQL = "SHOW COLUMNS FROM Table";
            //put all of the column names in the array
            foreach($conn->query($SQL) as $row) {
                $here[] = $row[0];
            }
        //now search through the array containing the column names for the name of the column, in this case i used the common ID field as an example
        $key = array_search('ID', $here);
        //now delete the entry
        unset($here[$key]);
    
    0 讨论(0)
  • 2020-11-22 01:28

    The answer posted by Mahomedalid has a small problem:

    Inside replace function code was replacing "<columns_to_delete>," by "", this replacement has a problem if the field to replace is the last one in the concat string due to the last one doesn't have the char comma "," and is not removed from the string.

    My proposal:

    SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME),
                      '<columns_to_delete>', '\'FIELD_REMOVED\'')
               FROM INFORMATION_SCHEMA.COLUMNS
               WHERE TABLE_NAME = '<table>'
                 AND TABLE_SCHEMA = '<database>'), ' FROM <table>');
    

    Replacing <table>, <database> and `

    The column removed is replaced by the string "FIELD_REMOVED" in my case this works because I was trying to safe memory. (The field I was removing is a BLOB of around 1MB)

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

    The accepted answer has several shortcomings.

    • It fails where the table or column names requires backticks
    • It fails if the column you want to omit is last in the list
    • It requires listing the table name twice (once for the select and another for the query text) which is redundant and unnecessary
    • It can potentially return column names in the wrong order

    All of these issues can be overcome by simply including backticks in the SEPARATOR for your GROUP_CONCAT and using a WHERE condition instead of REPLACE(). For my purposes (and I imagine many others') I wanted the column names returned in the same order that they appear in the table itself. To achieve this, here we use an explicit ORDER BY clause inside of the GROUP_CONCAT() function:

    SELECT CONCAT(
        'SELECT `',
        GROUP_CONCAT(COLUMN_NAME ORDER BY `ORDINAL_POSITION` SEPARATOR '`,`'),
        '` FROM `',
        `TABLE_SCHEMA`,
        '`.`',
        TABLE_NAME,
        '`;'
    )
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE `TABLE_SCHEMA` = 'my_database'
        AND `TABLE_NAME` = 'my_table'
        AND `COLUMN_NAME` != 'column_to_omit';
    
    0 讨论(0)
提交回复
热议问题