Select all columns except one in MySQL?

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

    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

提交回复
热议问题