SELECT * 列的 MySQL 别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8341136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL alias for SELECT * columns
提问by Pioul
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 columns. If i were to do it, i'd write it as:
因此,我需要为这些列提供别名。如果我要这样做,我会把它写成:
SELECT u.* as 'one_*', u2.* as 'two_*'
FROM users u
LEFT JOIN relationships r ON u.id=r.id_one
LEFT JOIN users u2 ON r.id_two=u2.id
But that doesn't work. Thanks for your help!
但这不起作用。谢谢你的帮助!
EDIT:
编辑:
Here's the data i'm actually getting:
这是我实际得到的数据:
| id | name | id | name |
1 john 2 alex
Here's the data i'd like to get (while still using a SELECT u.*, u2.*
):
这是我想要获得的数据(同时仍在使用 a SELECT u.*, u2.*
):
| id | name | brother_id | brother_name |
1 john 2 alex
采纳答案by Nonym
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
回答by Matt Healy
回答by Cylindric
Can you not just use SELECT *
and then in your code refer to u.field1
and u2.field2
?
你不能只使用SELECT *
然后在你的代码中引用u.field1
andu2.field2
吗?
回答by Max Width
SELECT alias.* does certainly work in mysql >= 5.6
SELECT alias.* 确实适用于 mysql >= 5.6