Select all columns except one in MySQL?

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

    I liked the answer from @Mahomedalid besides this fact informed in comment from @Bill Karwin. The possible problem raised by @Jan Koritak is true I faced that but I have found a trick for that and just want to share it here for anyone facing the issue.

    we can replace the REPLACE function with where clause in the sub-query of Prepared statement like this:

    Using my table and column name

    SET @SQL = CONCAT('SELECT ', (SELECT GROUP_CONCAT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'users' AND COLUMN_NAME NOT IN ('id')), ' FROM users');
    PREPARE stmt1 FROM @SQL;
    EXECUTE stmt1;
    

    So, this is going to exclude only the field id but not company_id

提交回复
热议问题