避免子查询中命名表的“as”?
大约一年前,我收到了mysql上一个问题的多个有效答案。
我将从两个答案中提取一个片段并标记它们,而不是复制整个查询并占用空间(除非你需要我)
两个答案都奏效了。
答案1
使用的第一个答案 AS
在命名表别名之前, AS highestcost
以及 AS lowestcost
...
...
FROM vendor
inner join vendorparts on vendor.id = vendorparts.vendorid
inner join part on vendorparts.partid = part.id
inner join product on product.partid = part.id
INNER JOIN (SELECT vendorparts.partid,
Max(vendorparts.lastcost) AS Highestcost
FROM vendorparts
GROUP BY vendorparts.partid) AS highestcost
ON part.id = highestcost.partid
INNER JOIN (SELECT vendorparts.partid,
Min(vendorparts.lastcost) AS Lowestcost
FROM vendorparts
GROUP BY vendorparts.partid) AS lowestcost
ON part.id = lowestcost.partid
WHERE vendorparts.lastcost <> 0
答案2
第二个答案没有用 AS
相反,他们只是在右括号和别名之间增加了一些额外的间距。他们还重新命名了表 t1
以及 t2
因此,为了避免任何列名称的歧义,我肯定会开始这样做,因为它的杂乱无章得多。
...
FROM vendor
INNER JOIN vendorparts on vendor.id = vendorparts.vendorid
INNER JOIN part on vendorparts.partid = part.id and
INNER JOIN product on product.partid = part.id
INNER JOIN (SELECT vendorparts.partid,
Max(vendorparts.lastcost) AS Highestcost
FROM vendorparts
GROUP BY vendorparts.partid) t1
ON part.id = t1.partid
INNER JOIN (SELECT vendorparts.partid,
Min(vendorparts.lastcost) AS Lowestcost
FROM vendorparts
GROUP BY vendorparts.partid) t2
ON part.id = t2.partid
WHERE vendorparts.lastcost <> 0
为什么?
两个答案都有效,第二个答案指定为“避免使用” AS
,但为什么?
我一直在用 AS
对于派生表名,已经有一段时间没有遇到任何问题了。mysql本身展示了 AS
在他们的衍生表的文档中。
这归结于个人喜好吗?更好的表现?如果有关系的话,第二个答案是由一个拥有80000+声望的用户提供的,只有一个问题。所以我认为他是对的。也许我没有正确地阅读mysql文档。
暂无答案!
目前还没有任何答案,快来回答吧!