SQL select join: is it possible to prefix all columns as 'prefix.*'?

后端 未结 22 1739
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 06:20

I\'m wondering if this is possible in SQL. Say you have two tables A and B, and you do a select on table A and join on table B:

SELECT a.*, b.* FROM TABLE_A a         


        
相关标签:
22条回答
  • 2020-12-02 06:50

    The only database I know that does this is SQLite, depending on the settings you configure with PRAGMA full_column_names and PRAGMA short_column_names. See http://www.sqlite.org/pragma.html

    Otherwise all I can recommend is to fetch columns in a result set by ordinal position rather than by column name, if it's too much trouble for you to type the names of the columns in your query.

    This is a good example of why it's bad practice to use SELECT * -- because eventually you'll have a need to type out all the column names anyway.

    I understand the need to support columns that may change name or position, but using wildcards makes that harder, not easier.

    0 讨论(0)
  • 2020-12-02 06:53

    DIfferent database products will give you different answers; but you're setting yourself up for hurt if you carry this very far. You're far better off choosing the columns you want, and giving them your own aliases so the identity of each column is crystal-clear, and you can tell them apart in the results.

    0 讨论(0)
  • 2020-12-02 06:53

    In postgres, I use the json functions to instead return json objects.... then, after querying, I json_decode the fields with a _json suffix.

    IE:

    select row_to_json(tab1.*),tab1_json, row_to_json(tab2.*) tab2_json 
     from tab1
     join tab2 on tab2.t1id=tab1.id
    

    then in PHP (or any other language), I loop through the returned columns and json_decode() them if they have the "_json" suffix (also removing the suffix. In the end, I get an object called "tab1" that includes all tab1 fields, and another called "tab2" that includes all tab2 fields.

    0 讨论(0)
  • 2020-12-02 06:54

    There is no SQL standard for this.

    However With code generation (either on demand as the tables are created or altered or at runtime), you can do this quite easily:

    CREATE TABLE [dbo].[stackoverflow_329931_a](
        [id] [int] IDENTITY(1,1) NOT NULL,
        [col2] [nchar](10) NULL,
        [col3] [nchar](10) NULL,
        [col4] [nchar](10) NULL,
     CONSTRAINT [PK_stackoverflow_329931_a] PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    CREATE TABLE [dbo].[stackoverflow_329931_b](
        [id] [int] IDENTITY(1,1) NOT NULL,
        [col2] [nchar](10) NULL,
        [col3] [nchar](10) NULL,
        [col4] [nchar](10) NULL,
     CONSTRAINT [PK_stackoverflow_329931_b] PRIMARY KEY CLUSTERED 
    (
        [id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    DECLARE @table1_name AS varchar(255)
    DECLARE @table1_prefix AS varchar(255)
    DECLARE @table2_name AS varchar(255)
    DECLARE @table2_prefix AS varchar(255)
    DECLARE @join_condition AS varchar(255)
    SET @table1_name = 'stackoverflow_329931_a'
    SET @table1_prefix = 'a_'
    SET @table2_name = 'stackoverflow_329931_b'
    SET @table2_prefix = 'b_'
    SET @join_condition = 'a.[id] = b.[id]'
    
    DECLARE @CRLF AS varchar(2)
    SET @CRLF = CHAR(13) + CHAR(10)
    
    DECLARE @a_columnlist AS varchar(MAX)
    DECLARE @b_columnlist AS varchar(MAX)
    DECLARE @sql AS varchar(MAX)
    
    SELECT @a_columnlist = COALESCE(@a_columnlist + @CRLF + ',', '') + 'a.[' + COLUMN_NAME + '] AS [' + @table1_prefix + COLUMN_NAME + ']'
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = @table1_name
    ORDER BY ORDINAL_POSITION
    
    SELECT @b_columnlist = COALESCE(@b_columnlist + @CRLF + ',', '') + 'b.[' + COLUMN_NAME + '] AS [' + @table2_prefix + COLUMN_NAME + ']'
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = @table2_name
    ORDER BY ORDINAL_POSITION
    
    SET @sql = 'SELECT ' + @a_columnlist + '
    ,' + @b_columnlist + '
    FROM [' + @table1_name + '] AS a
    INNER JOIN [' + @table2_name + '] AS b
    ON (' + @join_condition + ')'
    
    PRINT @sql
    -- EXEC (@sql)
    
    0 讨论(0)
  • 2020-12-02 06:55

    I see two possible situations here. First, you want to know if there is a SQL standard for this, that you can use in general regardless of the database. No, there is not. Second, you want to know with regard to a specific dbms product. Then you need to identify it. But I imagine the most likely answer is that you'll get back something like "a.id, b.id" since that's how you'd need to identify the columns in your SQL expression. And the easiest way to find out what the default is, is just to submit such a query and see what you get back. If you want to specify what prefix comes before the dot, you can use "SELECT * FROM a AS my_alias", for instance.

    0 讨论(0)
  • 2020-12-02 06:56

    If concerned about schema changes this might work for you: 1. Run a 'DESCRIBE table' query on all tables involved. 2. Use the returned field names to dynamically construct a string of column names prefixed with your chosen alias.

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