MySQL 更好的方法来选择第一个表中的所有列,并在内连接中选择第二个表中的一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14000572/
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
Better way to select all columns from first table and only one column from second table on inner join
提问by Zamicol
Graphical Explaination
图解说明
Table 1's columns:
表 1 的列:
|a|b|c|d|e|
Table 2's columns:
表 2 的列:
|a|x|y|z|
I want only a, b, c, d, e, x. I only want column 'a' from table 1, not column 'a' from table 2.
我只想要 a、b、c、d、e、x。我只想要表 1 中的“a”列,而不是表 2 中的“a”列。
Wordy Explaination
罗嗦解释
I have two tables with one column sharing a common name. If I use a Select * and use an inner join, I get all the columns returned including two columns with the same name.
我有两张表,其中一列共享一个通用名称。如果我使用 Select * 并使用内部联接,则会返回所有列,包括具有相同名称的两列。
I want to select everything from the first table, and only one column from the second table. Right now I am specifing every single column I need, which is a drag. Is there an easier way to grab everything from the first table and only the one column I want from the second table?
我想从第一个表中选择所有内容,从第二个表中只选择一列。现在我正在指定我需要的每一列,这是一个拖累。有没有更简单的方法可以从第一个表中获取所有内容,而从第二个表中只获取我想要的一列?
Thanks in advance.
提前致谢。
回答by John Woo
you need to specify the column name of the second table if it has the same columnName with the other table, you need to supply an ALIAS
on it,
您需要指定第二个表的列名,如果它与另一个表具有相同的 columnName,则需要在其ALIAS
上提供一个,
SELECT tb1.*, tb2.x
FROM tableA tb1
INNER JOIN tableB tb2
ON tb1.a = tb2.a