试图在SQL中使用表别名

时间:2010-06-14 21:00:46

标签: sql alias

我是一名平面设计师,我尽力理解表别名,但它不起作用。 这是我到目前为止所做的:

SELECT colours.colourid                  AS colourid1,
       combinations.manufacturercolourid AS colourmanid1,
       colours.colourname                AS colourname1,
       colours.colourhex                 AS colourhex1,
       combinations.qecolourid2          AS colouridqe2,
       colours.colourid                  AS colourid2,
       colours.colourname                AS colourname2,
       colours.colourhex                 AS colourhex2,
       colours.colourid                  AS colourid3,
       combinations.qecolourid3          AS colouridqe3,
       colours.colourname                AS colourname3,
       colours.colourhex                 AS colourhex3,
       colours.colourid                  AS colourid4,
       combinations.qecolourid4          AS colouridqe4,
       colours.colourname                AS colourname4,
       colours.colourhex                 AS colourhex4,
       combinations.coloursupplierid
FROM   combinations
       INNER JOIN colours
         ON colours.colourid = combinations.manufacturercolourid; 

现在,我的想法是在颜色查找表中,id将从查找表中提取颜色代码,十六进制和名称,以便我可以为我所选择的4种颜色提取颜色代码,十六进制和名称寻找。我可以让它工作,但它只提取名字,代码和十六进制,我只是没有看到我做错了。

4 个答案:

答案 0 :(得分:3)

您可以使用表别名来减少所需的输入量 - 通过添加以下内容:

SELECT 
   cl.colourid                  AS colourid1,
   cb.manufacturercolourid AS colourmanid1,
   cl.colourname                AS colourname1,
    ... and so on.....
FROM   
   combinations AS cb
INNER JOIN 
   colours AS cl ON cl.colourid = cb.manufacturercolourid; 

通过为表cb定义表别名combinations,您可以在SELECT和语句的其他部分使用较短的别名,而不必总是拼出整个表名。

但你的问题确实存在于JOIN中 - 你只加入一次,但你希望得到四个结果....

你需要做的是这样的事情:

SELECT 
   col1.colourid            AS colourid1,
   cb.manufacturercolourid  AS colourmanid1,
   col1.colourname          AS colourname1,
   col1.colourhex           AS colourhex1,

   cb.qecolourid2           AS colouridqe2,
   col2.colourid            AS colourid2,
   col2.colourname          AS colourname2,
   col2.colourhex           AS colourhex2,
   col2.colourid            AS colourid3,

   cb.qecolourid3           AS colouridqe3,
   col3.colourname          AS colourname3,
   col3.colourhex           AS colourhex3,
   col3.colourid            AS colourid4,

   cb.qecolourid4           AS colouridqe4,
   col4.colourname          AS colourname4,
   col4.colourhex           AS colourhex4,
   cb.coloursupplierid
FROM   
    combinations cb
INNER JOIN colours AS col1 ON col1.colourid = cb.manufacturercolourid
INNER JOIN colours AS col2 ON col2.colourid = cb.qecolourid2
INNER JOIN colours AS col3 ON col3.colourid = cb.qecolourid3
INNER JOIN colours AS col4 ON col4.colourid = cb.qecolourid4

答案 1 :(得分:3)

您的问题是您只链接颜色表中的一条记录,因为您的SQL中只有一个JOIN。该记录将与manufacturer_colour_id指定的颜色匹配。

您可能还有一个问题,即您的组合表似乎没有处于正常的正常形式(尽管我可能错了,不知道您尝试表示的数据的实际性质)。

如果我正确理解您的问题,解决方案(使用您当前的表结构)将更像是:

SELECT C1.colourid              AS colourid1,
   CMB.manufacturercolourid     AS colourmanid1,
   C1.colourname                AS colourname1,
   C1.colourhex                 AS colourhex1,
   CMB.qecolourid2              AS colouridqe2,
   C2.colourid                  AS colourid2,
   C2.colourname                AS colourname2,
   C2.colourhex                 AS colourhex2,
   C3.colourid                  AS colourid3,
   CMB.qecolourid3              AS colouridqe3,
   C3.colourname                AS colourname3,
   C3.colourhex                 AS colourhex3,
   C4.colourid                  AS colourid4,
   CMB.qecolourid4              AS colouridqe4,
   C4.colourname                AS colourname4,
   C4.colourhex                 AS colourhex4,
   CMB.coloursupplierid
FROM   combinations CMB
   LEFT OUTER JOIN colours C1
     ON C1.colourid = CMB.manufacturercolourid
   LEFT OUTER JOIN colours C2
     ON C2.colourid = CMB.qecolourid2
   LEFT OUTER JOIN colours C3
     ON C3.colourid = CMB.qecolourid3
   LEFT OUTER JOIN colours C4
     ON C4.colourid = CMB.qecolourid4

这里发生的是我将颜色表连接四次,对于组合表中的每个colour_id字段一次。为此,我需要每次对表名进行别名,以便我知道在返回列列表中使用的四种可能颜色实例中的哪一种。此外,如果一个或多个colour_id列可能为空,我正在使用OUTER JOIN。如果在INNER JOIN中发生这种情况,则整行将从结果集中删除。

答案 2 :(得分:2)

这不是一个详尽的答案,但您的问题与您使用JOIN的方式有关。表和列别名不会影响输出结果集。

您选择了相同的字段名称四次,这就是您获得奇怪结果的原因。

答案 3 :(得分:0)

这些都很棒,但出于某种原因,当我尝试使用它们时,我在页面中收到错误:

[Microsoft] [ODBC Microsoft Access驱动程序]查询表达式中的语法错误(缺少运算符)

我想我现在理解如何使用表别名,但出于某种原因,即使我确定它应该有效,页面也不喜欢它。