简体   繁体   English

了解多重自我加入

[英]understanding multiple self join

I was practicing self join at SQLZOO and here's a thing I do not understand towards the following question(Q10): 我在SQLZOO上练习自我加入,这对以下问题(Q10)我不了解:

两张桌子看起来像这样

The question is: Find the routes involving two buses that can go from Craiglockhart to Sighthill. 问题是:查找涉及两条可以从Craiglockhart到Sighthill的巴士的路线。 Show the bus no. 显示巴士号码 and company for the first bus, the name of the stop for the transfer, and the bus no. 第一辆巴士的公司和公司,中转站的名称以及巴士号 and company for the second bus The Answer is : 和公司第二辆公共汽车的答案是:

SELECT DISTINCT a.num, a.company, stopb.name ,  c.num,  c.company
FROM route as a JOIN route as b
ON (a.company = b.company AND a.num = b.num)
JOIN ( route c JOIN route d ON (c.company = d.company AND c.num= d.num))
JOIN stops as stopa ON (a.stop = stopa.id)
JOIN stops as stopb ON (b.stop = stopb.id)
JOIN stops as stopc ON (c.stop = stopc.id)
JOIN stops as stopd ON (d.stop = stopd.id)
WHERE  stopa.name = 'Craiglockhart' AND stopd.name = 'Sighthill'
        AND  stopb.name = stopc.name
ORDER BY LENGTH(a. num), b.num, stopb.id, LENGTH(c.num), d.num

I am quite confused about the result of 我对结果感到很困惑

FROM route a JOIN route b ON (a.company = b.company AND a.num = b.num)
JOIN ( route c JOIN route d ON (c.company = d.company AND c.num= d.num))

The second JOIN doesn't contain ON, so what's the join criteria here? 第二个JOIN不包含ON,那么这里的联接标准是什么? what would be the result of this join? 这种加入的结果是什么?

The result is a cross product. 结果是叉积。 Every row from the left matched to every row on the right. 左边的每一行都匹配右边的每一行。

For this type of join, without an ON clause, we typically include the CROSS keyword before JOIN , to alert the future reader that the omission of the ON clause was intentional, and not an oversight. 对于没有ON子句的这种类型的JOIN ,我们通常在JOIN之前包括CROSS关键字,以提醒以后的读者,忽略ON子句是有意的,而不是疏忽大意。

SELECT ...
  FROM a 
 CROSS
  JOIN b
 ORDER BY ...

We get the same result if we specify ON expr with an expression that evaluates to true for every row. 如果将ON expr指定为对每一行求值为true的表达式,则将得到相同的结果。

SELECT ...
  FROM a 
  JOIN b
    ON 1=1
 ORDER BY ...

Result is the same as if we had omitted the ON clause entirely... a row from a matches each and every row from b . 结果是一样的,如果我们省略了ON从整个条款等一行a分别从每一行比赛和b

In this case, I tend to omit the ON clause (where the expression/condition always evaluates to TRUE) and specify CROSS JOIN instead. 在这种情况下,我倾向于忽略ON子句(在该子句中,表达式/条件始终为TRUE),而是指定CROSS JOIN I think that makes the intent more clear. 我认为这使意图更加明确。

MySQL is more liberal than some other databases, which require the CROSS keyword when the ON clause is omitted. MySQL比其他一些数据库更为自由,其他一些数据库在省略ON子句时需要使用CROSS关键字。

In MySQL, the keywords INNER and CROSS are optional, and have no influence. 在MySQL中,关键字INNERCROSS是可选的,并且没有影响。 That is, JOIN and INNER JOIN and CROSS JOIN are all synonymous. 也就是说, JOININNER JOINCROSS JOIN都是同义词。 So our usage style is informed by compatibility with other databases, and being mindful of a future reader that may be trying to decipher our statement. 因此,我们的使用方式是通过与其他数据库的兼容性来告知的,并谨记将来的读者可能会试图解读我们的陈述。

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

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