MySQL 是否可以在内部联接期间重命名联接的列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5994485/
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
Is it possible to rename a joined column during an inner join?
提问by John
Say I have two tables, owner
and dog
. Both have column name
, but I'd like to join them, so there is a problem since both tables have column name
. Can I rename (alias) the name
column in the dog table during the query?
假设我有两张桌子,owner
并且dog
. 两者都有 column name
,但我想加入它们,所以存在一个问题,因为两个表都有 column name
。我可以name
在查询期间重命名(别名)dog 表中的列吗?
回答by RedFilter
select d.Name as DogName, o.Name
from Dog d
inner join Owner o on d.OwnerID = o.OwnerID
回答by mellamokb
Yes, you can, but then you must list out all of the fields instead of using select *
:
是的,您可以,但是您必须列出所有字段,而不是使用select *
:
select o.*, d.*
from owner o
inner join (select dog_id, name as dog_name, breed, age, owner_id from dog) d
on o.owner_id = d.owner_id
回答by Sanjay
You Can Give any Alias name to the column but there is some rules like the alias name must not the key word of the SQL Server, it must not contain space, if you want space then it should be in [] you can not use some symbols for alias.
您可以为列指定任何别名,但有一些规则,例如别名不能是 SQL Server 的关键字,不能包含空格,如果您想要空格,则它应该在 [] 中,您不能使用某些别名的符号。
Ex :- Select owner.Id As [Int], Owner.,dog.From Owner inner join Gog On Owner.Id = Dog.Id
例如:- 选择 owner.Id 作为 [Int], Owner。,狗。从所有者内部加入 Gog On Owner.Id = Dog.Id
回答by MJB
Yes, you can rename the columns in the output of a join, that is called an alias. However, the fact that they are the same does not cause any problem; they just need to be fully qualified.
是的,您可以重命名连接输出中的列,这称为别名。但是,它们相同的事实不会造成任何问题。他们只需要完全合格。