Hide field from select mysql

Asked

Viewed 217 times

2

Well I’m used to doing the select as follows:

select nome, idade,cidade from cadastro

This select me returns the fields name, age, city.

But I want to do the opposite, I have a table with many fields and I want select to return me all fields, except the fields nome and idade.

It is possible to do this?

  • 1

    I think you will have to discriminate the fields, except these 2 ai in the query.

  • I know that, but I want to know if there’s a simpler way. For I would discriminate only 2 fields that I didn’t want instead of having to discriminate all fields.

2 answers

2

You need permissions, there is no specific command but you can do so:

SET @sql = CONCAT('SELECT ', (SELECT REPLACE(GROUP_CONCAT(COLUMN_NAME), 
'<colunas_omitidas>,', '') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '<table>' 
AND TABLE_SCHEMA = '<database>'), ' FROM <table>');

    PREPARE stmt1 FROM @sql;
    EXECUTE stmt1;

Exchanging <table>, <database> and <colunas_omitidas>

I do a lot of work because there are tables with more than 60 columns

1

You can’t do this because there is no Modifier with this functionality provided for in SELECT, at least not until version 5.7:

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
      [PARTITION partition_list]
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name'
        [CHARACTER SET charset_name]
        export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

Reference: https://dev.mysql.com/doc/refman/5.7/en/select.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.