如何定义SQL查询以"从表中查找并返回值两次"

Al *_*och 0 sql

我不知道如何制定查询.为了简化问题,我创建了一个愚蠢而简单的例子.以下是具体内容:

  • TableColor:这是一个颜色表,如下所示:
ColorID       Color
1             Red
2             Green
3             Blue
Run Code Online (Sandbox Code Playgroud)
  • TableClothes:这是一张记录每天穿着的裤子和衬衫颜色的表格,如下所示:
Day       PantsColorID        ShirtColorID
  1       2                   3           (Day 1 wore green pants and a blue shirt)
  2       3                   1           (Day 2 wore blue pants and a red shirt)
Run Code Online (Sandbox Code Playgroud)

如何定义查询以返回如下所示的数据集:

Day   PantsColorID    PantsColor  ShirtColorID    ShirtColor
1     2               Green       3               Blue
2     3               Blue        1               Red
Run Code Online (Sandbox Code Playgroud)

这个查询让我很接近:

select 
    TableClothes.Day, 
    TableClothes.PantsColorID, 
    TableColor.Color as 'color of pants',
    TableClothes.ShirtColorID, 
    TableColor.Color as 'color of shirt', 
    TableColor.ColorID  
from TableClothes, TableColor 
where TableClothes.PantsColorID = TableColor.ColorID
Run Code Online (Sandbox Code Playgroud)
Day   PantsColorID    PantsColor  ShirtColorID    ShirtColor
1     2               Green       3               Green
2     3               Blue        1               Blue
Run Code Online (Sandbox Code Playgroud)

当然,此查询返回正确的裤子颜色,但显示与衬衫颜色相同的颜色,这是错误的.

如何构建查询以返回裤子和衬衫的正确颜色?

谢谢.

Zan*_*ane 7

首先.您正在使用旧式连接,这些连接因所有这些原因而不好.

其次,你有空格的标识符(不是最好的选择,你可以使用下划线而不是空格)更糟糕的是,你使用单引号作为这些标识符:'color of pants'这不是ANSI标准,因为单引号用于字符串而非常混乱文字以及弃用.看另一组好理由.所以,最好使用标识符双引号(或支架)"color of pants".

第三,我们将添加语句separator(;),因为它应该在语句结束的地方显而易见,并且因为SQL-Server很乐意允许你不放置这些分隔符并且做脏工作以找出语句结束的位置和另一个开始时,当下一个语句开始时它会变得混乱WITH.帮助他(以及将要阅读您的代码的下一位开发人员)保持理智.

因此,如果我们修复这些问题,您的查询将如下所示:

select 
    TableClothes.Day, 
    TableClothes.PantsColorID, 
    TableColor.Color as "color of pants",
    TableClothes.ShirtColorID, 
    TableColor.Color as "color of shirt", 
    TableColor.ColorID  
from TableClothes
inner join TableColor 
    on TableClothes.PantsColorID = TableColor.ColorID ;
Run Code Online (Sandbox Code Playgroud)

这不是给你你想要的结果的原因是你还需要加入衬衫和裤子colorID,这样你就可以获得两者的描述符信息.

select 
    TableClothes.Day, 
    TableClothes.PantsColorID, 
    TableColor.Color as "color of pants",
    TableClothes.ShirtColorID, 
    TableColor.Color as "color of shirt", 
    TableColor.ColorID  
from TableClothes
inner join TableColor 
    on TableClothes.PantsColorID = TableColor.ColorID
inner join TableColor   
    on TableClothes.ShirtColorID = TableColor.ColorID
Run Code Online (Sandbox Code Playgroud)

哦,但等待不编译.那是因为当您像这样两次引用TableColor时,数据库系统不知道您在SELECTJOIN语句中引用哪一个.因此,我们将使用一种称为别名的技术,这种技术不仅可以解决这个问题,还可以使代码更易于阅读.

select 
    C.Day, 
    C.PantsColorID, 
    P.Color as 'color of pants',
    C.ShirtColorID, 
    S.Color as 'color of shirt', 
    C.ColorID  
from TableClothes as C
inner join TableColor as P
    on C.PantsColorID = P.ColorID
inner join TableColor as S
    on C.ShirtColorID = S.ColorID ;
Run Code Online (Sandbox Code Playgroud)

现在我们有一个功能简洁易读的查询.