Select all columns except one in MySQL?

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

    You could use DESCRIBE my_table and use the results of that to generate the SELECT statement dynamically.

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

    I agree with the "simple" solution of listing all the columns, but this can be burdensome, and typos can cause lots of wasted time. I use a function "getTableColumns" to retrieve the names of my columns suitable for pasting into a query. Then all I need to do is to delete those I don't want.

    CREATE FUNCTION `getTableColumns`(tablename varchar(100)) 
              RETURNS varchar(5000) CHARSET latin1
    BEGIN
      DECLARE done INT DEFAULT 0;
      DECLARE res  VARCHAR(5000) DEFAULT "";
    
      DECLARE col  VARCHAR(200);
      DECLARE cur1 CURSOR FOR 
        select COLUMN_NAME from information_schema.columns 
        where TABLE_NAME=@table AND TABLE_SCHEMA="yourdatabase" ORDER BY ORDINAL_POSITION;
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
      OPEN cur1;
      REPEAT
           FETCH cur1 INTO col;
           IF NOT done THEN 
              set res = CONCAT(res,IF(LENGTH(res)>0,",",""),col);
           END IF;
        UNTIL done END REPEAT;
      CLOSE cur1;
      RETURN res;
    

    Your result returns a comma delimited string, for example...

    col1,col2,col3,col4,...col53

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

    Based on @Mahomedalid answer, I have done some improvements to support "select all columns except some in mysql"

    SET @database    = 'database_name';
    SET @tablename   = 'table_name';
    SET @cols2delete = 'col1,col2,col3';
    
    SET @sql = CONCAT(
    'SELECT ', 
    (
        SELECT GROUP_CONCAT( IF(FIND_IN_SET(COLUMN_NAME, @cols2delete), NULL, COLUMN_NAME ) )
        FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @tablename AND TABLE_SCHEMA = @database
    ), 
    ' FROM ',
    @tablename);
    
    SELECT @sql;
    

    If you do have a lots of cols, use this sql to change group_concat_max_len

    SET @@group_concat_max_len = 2048;
    
    0 讨论(0)
  • 2020-11-22 01:20

    May be I have a solution to Jan Koritak's pointed out discrepancy

    SELECT CONCAT('SELECT ',
    ( SELECT GROUP_CONCAT(t.col)
    FROM
    (
        SELECT CASE
        WHEN COLUMN_NAME = 'eid' THEN NULL
        ELSE COLUMN_NAME
        END AS col 
        FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_NAME = 'employee' AND TABLE_SCHEMA = 'test'
    ) t
    WHERE t.col IS NOT NULL) ,
    ' FROM employee' );
    

    Table :

    SELECT table_name,column_name 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_NAME = 'employee' AND TABLE_SCHEMA = 'test'
    

    ================================

    table_name  column_name
    employee    eid
    employee    name_eid
    employee    sal
    

    ================================

    Query Result:

    'SELECT name_eid,sal FROM employee'
    
    0 讨论(0)
  • 2020-11-22 01:21

    At first I thought you could use regular expressions, but as I've been reading the MYSQL docs it seems you can't. If I were you I would use another language (such as PHP) to generate a list of columns you want to get, store it as a string and then use that to generate the SQL.

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

    You can do:

    SELECT column1, column2, column4 FROM table WHERE whatever
    

    without getting column3, though perhaps you were looking for a more general solution?

    0 讨论(0)
提交回复
热议问题