MySQL alias for SELECT * columns

邮差的信 提交于 2019-11-28 22:30:48

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

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

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!