【发布时间】:2019-04-13 12:30:34
【问题描述】:
假设我有两张表 Customers$、Orders$。我想知道这两个(如下所示)给出相同的结果。那么,我可以使用其中任何一种方法或第二种方法来代替内部连接吗?
select Orders$.OrderDate,
Customers$.ContactName
from Orders$
inner join Customers$ on Orders$.CustomerID = Customers$.CustomerID;
select Orders$.OrderDate,
Customers$.ContactName
from Orders$,Customers$
where Orders$.CustomerID = Customers$.CustomerID;
但是当我解决一个问题时—— https://www.hackerrank.com/challenges/average-population-of-each-continent/problem
所以两者都应该可以正常工作,但第二个不能正常工作,正如你在评论中所说的那样,这两个都是一样的
select country.continent , round(avg(city.population -.5 , 0)) from country, city where country.code=city.countrycode group by country.continent;
select country.continent, round(avg(city.population - .5),0) from country inner join city on country.code=city.countrycode group by country.continent
【问题讨论】:
-
是的,但您为什么要故意选择使用 1980 年代的语法?现在是 2019 年。ANSI92 JOIN 语法已经存在了 27 年,没有理由不使用它。
-
这并不能解释为什么您会使用 30 年的编码实践,而不是仅在 3 年后取代它们的新标准。 ;)
-
第二个 2 语句不等价。
round(avg(city.population -.5 , 0))round(avg(city.population),0)。另外,我强烈建议在编写 SQL 时使用空格和缩进;单行代码很难阅读(以我对您的第一条语句的操作为例)。 -
是的,谢谢,我做错了,找不到????。现在明白了
-
良好的格式使事情变得更容易的原因之一。错误更容易解决。尤其是如果错误生成语法错误,例如“第 1 行错误”。如果你的 SQL 是 1 行,它可能在任何地方,但是“第 275 行错误”给你一个更好的机会。
标签: sql-server join select syntax