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

时间:2015-02-20 20:45:14

标签: sql

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

  • TableColor:这是一个颜色表,如下所示:
ColorID       Color
1             Red
2             Green
3             Blue
  • 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)

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

Day   PantsColorID    PantsColor  ShirtColorID    ShirtColor
1     2               Green       3               Blue
2     3               Blue        1               Red

此查询让我接近:

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
Day   PantsColorID    PantsColor  ShirtColorID    ShirtColor
1     2               Green       3               Green
2     3               Blue        1               Blue

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

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

谢谢。

5 个答案:

答案 0 :(得分:7)

首先关闭。您正在使用对all of these reasons不利的旧式联接。

其次,你有空格的标识符(不是最好的选择,你可以使用下划线而不是空格)更糟糕的是,你使用单引号来表示这些标识符:'color of pants'这不是ANSI标准,单引号用于字符串文字以及弃用时非常混乱。见another set of good reasons。因此,最好使用双引号(或括号)作为标识符:"color of pants"

第三,我们将添加语句分隔符(;),因为它应该是语句结束的显而易见的地方,也因为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 ;

这并不是为您提供您渴望的结果的原因是您还需要加入衬衫和裤子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

哦,等等,不能编译。这是因为当您像这样两次引用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 ;

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

答案 1 :(得分:4)

您使用别名 - 如下所示:

select TableClothes.Day, 
       TableClothes.PantsColorID, 
       Color1.Color as 'color of pants' ,
       TableClothes.ShirtColorID, 
       Color2.Color as 'color of shirt', 
from TableClothes, TableColor as Color1, TableColor as Color2
where TableClothes.PantsColorID = Color1.ColorID
 and  TableClothes.ShirtColorID = Color2.ColorID

使用现代连接语法更常见(我认为这更清晰)

select TableClothes.Day, 
       TableClothes.PantsColorID, 
       TableColor.Color as 'color of pants' ,
       TableClothes.ShirtColorID, 
       Color2.Color as 'color of shirt', 
from TableClothes, 
join TableColor as Color1 on TableClothes.PantsColorID = Color1.ColorID
join TableColor as Color2 on TableClothes.ShirtColorID = Color2.ColorID

答案 2 :(得分:2)

您需要两次使用不同的条件和Colors表的不同别名,对Colorles表加入TableClothes表两次。

SELECT Day, 
PantsColorID, PantsColors.Color AS PantsColor, 
ShirtColorID, ShirtColors.Color AS ShirtColor
FROM TableClothes 
JOIN TableColor PantsColors ON TableClothes.PantsColorID = PantsColors.ColorID
JOIN TableColor ShirtColors ON TableClothes.ShirtColorID = ShirtColors.ColorID

答案 3 :(得分:-2)

SELECT TableClothes.Day, 
  TableClothes.PantsColorID, 
  t1.Color as PantsColor ,
  TableClothes.ShirtColorID, 
  t2.Color as ShirtColor,
  TableColor.ColorID  
from TableClothes

LEFT JOIN TableColor as t1
ON TableColor.ColorId = TableClothes.PantsColorID

LEFT JOIN TableColor as t2
ON TableColor.ColorId = TableClothes.ShirtColorID

答案 4 :(得分:-2)

您需要将Clothes表连接到Color表两次。像

select * from TableClothes L, TableColor C1, TableColor C2 where
L.shirtcolorid = c1.colorid and L.pantscolorid = c2.colorid