MySQL alias for SELECT * columns

后端 未结 4 1997
-上瘾入骨i
-上瘾入骨i 2020-12-05 17:40

I\'m creating a view that is using data that comes from the same table twice. As a result, same column names appear twice.

Thus, i need to give aliases to these colu

相关标签:
4条回答
  • 2020-12-05 17:47

    Can you not just use SELECT * and then in your code refer to u.field1 and u2.field2?

    0 讨论(0)
  • 2020-12-05 17:50

    You can't use * with an alias. Aliases can be used for individual columns.

    You'll have to alias each column instead..

    So unfortunately, if you have a lot of columns, you'll need to go:

    SELECT u.col1 AS u_col1
        , u.col2 AS u_col2
        , u.col3 AS u_col3
        -- etc
        , u2.col1 AS u2_col1
        , u2.col2 AS u2_col2
        , u2.col3 AS u2_col3
        -- etc
    FROM table1 AS u
    -- INNER JOIN / LEFT OR RIGHT OUTER JOIN / ,
        table2 AS u2
    
    0 讨论(0)
  • 2020-12-05 18:04

    Try using a UNION query:

    e.g.

    select a.typeid, a.typename from MYTABLE a where a.typeid=3 UNION select a.typeid, a.typename from MYTABLE a where a.typeid=4

    0 讨论(0)
  • 2020-12-05 18:07

    SELECT alias.* does certainly work in mysql >= 5.6

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