MySQL - 使用Join运算符后重复列 [英] MySQL - Duplicate columns after using Join operator
问题描述
如标题所述,我将使用 JOIN
查询获取重复的列。
给出几个表,我想写select select语句,只需要
从所需表的信息。
As stated in the title, I'm getting duplicate columns with this JOIN
query.
A few tables are given and I want to write select statements to get only
the information from the tables which are needed.
这是我的SQL代码到目前为止:
Here is my SQL code so far:
SELECT mitarbeiter.PNR, pfleger.PNR, Name
from pfleger
JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR)
where Ort='Frankfurt';
执行后,我得到以下结果:
After executing, I get the following result:
您可以看到问题:我有两个PNR列,我不想拥有。
如何删除重复?我尝试过 SELECT DISTINCT ...
,但没有完成我的目标。
You can see the problem: I have two PNR columns which I don't want to have.
How can I remove the duplicate? I have tried SELECT DISTINCT ...
but it doesn't accomplish my goal.
推荐答案
正如其他用户已经提到的,您只需从 SELECT
子句中删除一个字段名称。我只是想补充一点,如果你加入的字段在两个表中都有相同的名称,你可以使用特殊的语法,这样可以将这两个列作为一个引用引用:
As other users have already mentioned, you just need to remove a field name from the SELECT
clause. I just want to add that if the field you join on has the same name in both tables you can use special syntax, which allows to reference both columns as a single one:
SELECT PNR, Name
from pfleger
JOIN mitarbeiter USING (PNR)
where Ort='Frankfurt';
这篇关于MySQL - 使用Join运算符后重复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!