简体   繁体   English

SQL LEFT JOIN 和别名问题

[英]SQL LEFT JOIN and alias questions

Here is part of my query.这是我的查询的一部分。

        , dbo.tew_translatedtext.tra_0  as FromParentTagDescription
        , dbo.tew_translatedtext.tra_0  as ToParentTagDescription
    , rrr.tra_0 as ToParentTagDescription

FROM vew_cable_ex  -- as test alias

LEFT JOIN vew_wire_ex  ON ((vew_wire_ex.cab_id = vew_cable_ex.cab_id) AND vew_wire_ex.caw_no = vew_cable_ex.caw_no)

    -- get the parent tag description
    left join dbo.tew_translatedtext   on ( vew_wire_ex.vwircomcomfrom_com_id = dbo.tew_translatedtext.tra_objectid )
    left join dbo.tew_translatedtext  as rrr on ( vew_wire_ex.vwircomcomto_com_id = dbo.tew_translatedtext.tra_objectid )

I have few questions here.我在这里有几个问题。 - is the 'as rrr' used properly? - 'as rrr' 使用得当吗? - The script could run. - 脚本可以运行。 But seems 'rrr.tra_0 as ToParentTagDescription' didn't give the expected result.但似乎 'rrr.tra_0 as ToParentTagDescription' 没有给出预期的结果。 all empty.都是空的。 i suspect that the SQL syntax somewhere is incorrect.我怀疑某处的 SQL 语法不正确。

Any comment is appreciated.任何评论表示赞赏。

best Roland最好的罗兰

The best practice is to assign all of your tables meaningful aliases, then reference those aliases every time you identify a column in your query.最佳做法是为所有表分配有意义的别名,然后在每次识别查询中的列时引用这些别名。 That will help make it clear, both as you write, and when you or someone else has to come back and troubleshoot, which table each column comes from.这将有助于在您编写时以及当您或其他人必须回来进行故障排除时清楚地说明每列来自哪个表。 It also makes the code easier to read.它还使代码更易于阅读。

Once you've aliased the table, many SQL dialects require that you then use the alias in the JOIN...ON criteria.为表设置别名后,许多 SQL 方言要求您在JOIN...ON条件中使用别名。

I should note that in your code snippet, you have the column ToParentTagDescription defined twice, so I repeated that below.我应该注意,在您的代码片段中,您定义了两次ToParentTagDescription列,所以我在下面重复了这一点。 Are you sure you're looking at the right one for your expected results, though?不过,您确定您正在寻找适合您预期结果的产品吗? You're also calling the column tra_0 from your first join to tew_translatedtext twice and assigning different column aliases to it.您还从第一次连接到tew_translatedtext两次调用列tra_0并为其分配不同的列别名。

As a matter of readability, I find parenthesis in joins to be visual clutter unless they're necessary for actual grouping of some sort, so I removed them.作为可读性的问题,我发现连接中的括号是视觉上的混乱,除非它们是某种实际分组所必需的,所以我删除了它们。

...
   fid.tra_0 AS FromParentTagDescription
  ,fid.tra_0 AS ToParentTagDescription
  ,tid.tra_0 AS ToParentTagDescription
FROM 
  <AddSchemaHere>.vew_cable_ex AS cx
LEFT JOIN 
  <AddSchemaHere>.vew_wire_ex AS wx
  ON 
    wx.cab_id = cx.cab_id
    AND 
    wx.caw_no = cx.caw_no
-- get the parent tag description
LEFT JOIN 
  dbo.tew_translatedtext AS fid --From ID...
    ON wx.vwircomcomfrom_com_id = fid.tra_objectid
LEFT JOIN 
  dbo.tew_translatedtext AS tid --To ID 
    ON wx.vwircomcomto_com_id = tid.tra_objectid;

The problem is that you're joining dbo.tew_translatedtext to your query twice, but you're not assigning aliases to each of them and you're not using the alias you have consistently.问题是您将dbo.tew_translatedtext加入到您的查询中两次,但是您没有为每个人分配别名,并且您没有使用始终如一的别名。 This means that the database engine you're using (I suspect it's SQL Server, but it would help if you would tag the database engine properly) is guessing about which of the table joins you meant - and it's probably getting it wrong.这意味着您正在使用的数据库引擎(我怀疑它是 SQL 服务器,但如果您正确标记数据库引擎会有所帮助)正在猜测您的意思是连接哪个表 - 它可能弄错了。 I suspect that you meant something like:我怀疑你的意思是:

  .
  .
  .
  , fp.tra_0 as FromParentTagDescription
  , tp.tra_0 as ToParentTagDescription
FROM vew_cable_ex as cx
LEFT JOIN vew_wire_ex as vx
  ON (vx.cab_id = cx.cab_id AND
      vx.caw_no = cx.caw_no)
left join dbo.tew_translatedtext as fp -- get the parent tag description
  on (vx.vwircomcomfrom_com_id = fp.tra_objectid )
left join dbo.tew_translatedtext as tp
  on (vx.vwircomcomto_com_id = tp.tra_objectid )

Here I've used fp as an alias for "from parent", and tp as an alias to "to parent".在这里,我使用fp作为“from parent”的别名,并使用tp作为“to parent”的别名。 This seems to be in keeping with the column aliases assigned earlier.这似乎与之前分配的列别名一致。

enter image description here在此处输入图像描述

The last issue was fixed.最后一个问题已修复。

I'm trying to join another view as the screenshot shows.如屏幕截图所示,我正在尝试加入另一个视图。 left join vew_cable_ex as cex on cex.fun_bun_id = vew_bundle.bun_defaultfunid在 cex.fun_bun_id = vew_bundle.bun_defaultfuid 上左加入 vew_cable_ex 作为 cex

it throw a error, saying can't bound.它抛出一个错误,说不能绑定。 it seems it is related to vew_cable_ex table.它似乎与 vew_cable_ex 表有关。 Could you point the issue out?你能指出这个问题吗?

Regars, Roland问候,罗兰

some notes to my answer: in the ON clause on the table where you alias to rrr, you wrongly aliased the column it looks like.我的回答的一些注释:在您别名为 rrr 的表的 ON 子句中,您错误地为它看起来像的列别名。

  , dbo.tew_translatedtext.tra_0  as FromParentTagDescription
    , dbo.tew_translatedtext.tra_0  as ToParentTagDescription
, rrr.tra_0 as ToParentTagDescription

FROM vew_cable_ex  -- as test alias

 LEFT JOIN vew_wire_ex  ON ((vew_wire_ex.cab_id = vew_cable_ex.cab_id) AND 
 vew_wire_ex.caw_no = vew_cable_ex.caw_no)

-- get the parent tag description
left join dbo.tew_translatedtext   on ( vew_wire_ex.vwircomcomfrom_com_id = dbo.tew_translatedtext.tra_objectid )
left join dbo.tew_translatedtext  as rrr on ( vew_wire_ex.vwircomcomto_com_id = rrr.tra_objectid )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM