Select all columns except one in MySQL?

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

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

    My main problem is the many columns I get when joining tables. While this is not the answer to your question (how to select all but certain columns from one table), I think it is worth mentioning that you can specify table. to get all columns from a particular table, instead of just specifying .

    Here is an example of how this could be very useful:

    select users.*, phone.meta_value as phone, zipcode.meta_value as zipcode
    
    from users
    
    left join user_meta as phone
    on ( (users.user_id = phone.user_id) AND (phone.meta_key = 'phone') )
    
    left join user_meta as zipcode
    on ( (users.user_id = zipcode.user_id) AND (zipcode.meta_key = 'zipcode') )
    
    

    The result is all the columns from the users table, and two additional columns which were joined from the meta table.

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

    It is good practice to specify the columns that you are querying even if you query all the columns.

    So I would suggest you write the name of each column in the statement (excluding the one you don't want).

    SELECT
        col1
        , col2
        , col3
        , col..
        , col53
    
    FROM table
    
    0 讨论(0)
  • 2020-11-22 01:31

    Yes, though it can be high I/O depending on the table here is a workaround I found for it.

    SELECT *
    INTO #temp
    FROM table
    
    ALTER TABLE #temp DROP COlUMN column_name
    
    SELECT *
    FROM #temp
    
    0 讨论(0)
  • 2020-11-22 01:32

    To the best of my knowledge, there isn't. You can do something like:

    SELECT col1, col2, col3, col4 FROM tbl
    

    and manually choose the columns you want. However, if you want a lot of columns, then you might just want to do a:

    SELECT * FROM tbl 
    

    and just ignore what you don't want.

    In your particular case, I would suggest:

    SELECT * FROM tbl
    

    unless you only want a few columns. If you only want four columns, then:

    SELECT col3, col6, col45, col 52 FROM tbl
    

    would be fine, but if you want 50 columns, then any code that makes the query would become (too?) difficult to read.

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

    If you are looking to exclude the value of a field, e.g. for security concerns / sensitive info, you can retrieve that column as null.

    e.g.

    SELECT *, NULL AS salary FROM users
    
    0 讨论(0)
提交回复
热议问题