Select all columns except one in MySQL?

后端 未结 30 2923
后悔当初
后悔当初 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条回答
  •  梦毁少年i
    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'
    

提交回复
热议问题