SQL exclude a column using SELECT * [except columnA] FROM tableA?

后端 未结 30 2553
花落未央
花落未央 2020-11-21 23:15

We all know that to select all columns from a table, we can use

SELECT * FROM tableA

Is there a way to exclude column(s) from a table witho

相关标签:
30条回答
  • 2020-11-21 23:59

    A modern SQL dialect like BigQuery proposes an excellent solution

    SELECT * EXCEPT(ColumnNameX, [ColumnNameY, ...])

    This is a very powerful SQL syntax to avoid a long list of columns that need to be updated all the time due to table column name changes. And this functionality is missing in the current SQL Server implementation, which is a pity. Hopefully, one day, Microsoft Azure will be more data scientist friendly.

    Data scientists like to be able to have a quick option to shorten a query and be able to remove some columns (due to duplication or any other reason).

    https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select-modifiers

    0 讨论(0)
  • 2020-11-21 23:59

    In Hive Sql you can do this:

    set hive.support.quoted.identifiers=none;
    select 
        `(unwanted_col1|unwanted_col2|unwanted_col3)?+.+`
    from database.table
    

    this gives you the rest cols

    0 讨论(0)
  • 2020-11-22 00:00

    If you are using SQL Server Management Studio then do as follows:

    1. Type in your desired tables name and select it
    2. Press Alt+F1
    3. o/p shows the columns in table.
    4. Select the desired columns
    5. Copy & paste those in your select query
    6. Fire the query.

    Enjoy.

    0 讨论(0)
  • 2020-11-22 00:00

    The best way to solve this is using view you can create view with required columns and retrieve data form it

    example
    
    mysql> SELECT * FROM calls;
    +----+------------+---------+
    | id | date       | user_id |
    +----+------------+---------+
    |  1 | 2016-06-22 |       1 |
    |  2 | 2016-06-22 |    NULL |
    |  3 | 2016-06-22 |    NULL |
    |  4 | 2016-06-23 |       2 |
    |  5 | 2016-06-23 |       1 |
    |  6 | 2016-06-23 |       1 |
    |  7 | 2016-06-23 |    NULL |
    +----+------------+---------+
    7 rows in set (0.06 sec)
    
    mysql> CREATE VIEW C_VIEW AS
        ->     SELECT id,date from calls;
    Query OK, 0 rows affected (0.20 sec)
    
    mysql> select * from C_VIEW;
    +----+------------+
    | id | date       |
    +----+------------+
    |  1 | 2016-06-22 |
    |  2 | 2016-06-22 |
    |  3 | 2016-06-22 |
    |  4 | 2016-06-23 |
    |  5 | 2016-06-23 |
    |  6 | 2016-06-23 |
    |  7 | 2016-06-23 |
    +----+------------+
    7 rows in set (0.00 sec)
    
    0 讨论(0)
  • 2020-11-22 00:01

    Is there a way to exclude column(s) from a table without specifying all the columns?

    Using declarative SQL in the usual way, no.

    I think your proposed syntax is worthy and good. In fact, the relational database language 'Tutorial D' has a very similar syntax where the keywords ALL BUT are followed by a set of attributes (columns).

    However, SQL's SELECT * already gets a lot a flak (@Guffa's answer here is a typical objection), so I don't think SELECT ALL BUT will get into the SQL Standard anytime soon.

    I think the best 'work around' is to create a VIEW with only the columns you desire then SELECT * FROM ThatView.

    0 讨论(0)
  • 2020-11-22 00:03

    In SQL Management Studio you can expand the columns in Object Explorer, then drag the Columns tree item into a query window to get a comma separated list of columns.

    0 讨论(0)
提交回复
热议问题