简体   繁体   English

尝试加入两个派生表时出错

[英]Error when trying to JOIN two derived tables

I'm going nuts trying to figure out this error. 我疯了,试图找出这个错误。 I am working in MySQL and need to join two derived tables on a common column. 我在MySQL工作,需要在公共列上连接两个派生表。 All columns in the two tables are aliased as are the tables. 两个表中的所有列都是表格别名。

Scheme is 方案是

stops (id, name)

route (num, company, pos, stop) where route (num, company, pos, stop)在哪里

stops.id <---> route.stop stops.id <---> route.stop

These tables contain bus routes between cities and I want to figure out all the routes from 'Craiglockhart' to 'Sighthill' which require exactly two buses (ie, a transfer point). 这些表包含城市之间的公交路线,我想弄清楚从'Craiglockhart'到'Sighthill'的所有路线,这些路线只需要两条公交车(即转机点)。 Moreover, because the route table doesn't contain stop names (only ID's), we use some joins inside the derived tables to refer to stops by name; 此外,因为route表不包含停止名称(仅ID),我们在派生表中使用一些连接来引用按名称停止; this is just a convenience) 这只是一个方便)

So I tried to make two derived tables. 所以我试着制作两个派生表。 One of all the routes that can take a person between 'Craiglockhart' and any station which is not 'Craighartlock' and an identical second derived table but for 'Sighthill'. 其中一条路线可以让人们在'Craiglockhart'和任何不是'Craighartlock'的车站和一个相同的第二个衍生车牌之间,而是'Sighthill'。 I was able to get both such tables to work separately. 我能够让这两个表单独工作。

But, when I tried to join them along the common column of the so-called transfer station (ie, the non-Craighartlock station in the first table and the non-Sighthill station in the second), I get an error. 但是,当我试图沿着所谓的转运站的公共列(即第一个表中的非Craighartlock站和第二个表中的非Sighthill站)加入它们时,我收到错误。

Derived Table 1 : This works fine and returns the table correctly. 派生表1 :此工作正常并正确返回表。 here, stop_b.name represents the transfer station which is not Craiglockhart and stop_a.name represents the Craighill "starting" station. 这里, stop_b.name表示不是Craiglockhart的转移站, stop_a.name表示Craighill“起始”站。

SELECT * FROM 
    (SELECT a.num AS num_a, a.company AS comp_a, stop_a.name AS name_a, 
            stop_b.name AS name_transfer FROM
               route a JOIN route b ON (a.company=b.company AND a.num=b.num)
                    JOIN stops stop_a ON (a.stop=stop_a.id)
                    JOIN stops stop_b ON (b.stop=stop_b.id)
                    WHERE stop_a.name = 'Craiglockhart' AND 
                          stop_b.name <> 'Craiglockhart') AS first_route

Derived Table 2 : SAME THING but different table alias and station restriction. 派生表2 :相同但不同的表别名和站限制。 Here stop_b.name represents the transfer station (which is not Sighthill) and stop_a.name represents the Sighthill "ending" station. 这里stop_b.name表示转移站(不是Sighthill), stop_a.name表示Sighthill“结束”站。

SELECT * FROM 
    (SELECT a.num AS num_a, a.company AS comp_a, stop_a.name AS name_a, 
            stop_b.name AS name_transfer FROM
               route a JOIN route b ON (a.company=b.company AND a.num=b.num)
                    JOIN stops stop_a ON (a.stop=stop_a.id)
                    JOIN stops stop_b ON (b.stop=stop_b.id)
                    WHERE stop_a.name = 'Sighthill' AND 
                          stop_b.name <> 'Sighthill') AS second_route

But, when I try to join these along their common `name_transfer' column (alias for stop_b.name in both) I get an error: 但是,当我尝试将它们连接到它们共同的`name_transfer'列(两者中的stop_b.name的别名)时,我收到一个错误:

SELECT * FROM
(
SELECT * FROM 
    (SELECT a.num AS num_a, a.company AS comp_a, stop_a.name AS name_a, 
            stop_b.name AS name_transfer FROM
               route a JOIN route b ON (a.company=b.company AND a.num=b.num)
                    JOIN stops stop_a ON (a.stop=stop_a.id)
                    JOIN stops stop_b ON (b.stop=stop_b.id)
                    WHERE stop_a.name = 'Craiglockhart' AND 
                          stop_b.name <> 'Craiglockhart') AS first_route
JOIN

(SELECT * FROM 
    (SELECT a.num AS num_a, a.company AS comp_a, stop_a.name AS name_a, 
            stop_b.name AS name_transfer FROM
               route a JOIN route b ON (a.company=b.company AND a.num=b.num)
                    JOIN stops stop_a ON (a.stop=stop_a.id)
                    JOIN stops stop_b ON (b.stop=stop_b.id)
                    WHERE stop_a.name = 'Sighthill' AND 
                          stop_b.name <> 'Sighthill') AS second_route)

ON (first_route.name_transfer = second_route.name_transfer)
)

I also tried replacing ON with USING (name_transfer) since the column I want to join the derived tables on is called that in both of the derived tables. 我也尝试用USING (name_transfer)替换ON ,因为我要在两个派生表中调用我想要加入派生表的列。

Any help would be greatly appreciated! 任何帮助将不胜感激!

You need parentheses around a subquery when you use it in a FROM or JOIN clause. FROMJOIN子句中使用子查询时,需要围绕子查询使用括号。 You have it in the FROM clause, but not in the JOIN clause. 您在FROM子句中拥有它,但在JOIN子句中没有它。 The problem seems to have come when you added the unnecessary SELECT * FROM SELECT around each subquery. 当你在每个子查询周围添加不​​必要的SELECT * FROM SELECT时,问题似乎就出现了。

SELECT * FROM 
    (SELECT a.num AS num_a, a.company AS comp_a, stop_a.name AS name_a, 
            stop_b.name AS name_transfer FROM
               route a JOIN route b ON (a.company=b.company AND a.num=b.num)
                    JOIN stops stop_a ON (a.stop=stop_a.id)
                    JOIN stops stop_b ON (b.stop=stop_b.id)
                    WHERE stop_a.name = 'Craiglockhart' AND 
                          stop_b.name <> 'Craiglockhart') AS first_route
JOIN

    (SELECT a.num AS num_a, a.company AS comp_a, stop_a.name AS name_a, 
            stop_b.name AS name_transfer FROM
               route a JOIN route b ON (a.company=b.company AND a.num=b.num)
                    JOIN stops stop_a ON (a.stop=stop_a.id)
                    JOIN stops stop_b ON (b.stop=stop_b.id)
                    WHERE stop_a.name = 'Sighthill' AND 
                          stop_b.name <> 'Sighthill') AS second_route

ON first_route.name_transfer = second_route.name_transfer

Try this one. 试试这个吧。

SELECT * FROM
    (SELECT * FROM 
        (SELECT a.num AS num_a, a.company AS comp_a, stop_a.name AS name_a, 
                stop_b.name AS name_transfer FROM
                   route a JOIN route b ON (a.company=b.company AND a.num=b.num)
                        JOIN stops stop_a ON (a.stop=stop_a.id)
                        JOIN stops stop_b ON (b.stop=stop_b.id)
                        WHERE stop_a.name = 'Craiglockhart' AND 
                              stop_b.name <> 'Craiglockhart') AS first_route) AS A
    JOIN

    (SELECT * FROM 
        (SELECT a.num AS num_a, a.company AS comp_a, stop_a.name AS name_a, 
                stop_b.name AS name_transfer FROM
                   route a JOIN route b ON (a.company=b.company AND a.num=b.num)
                        JOIN stops stop_a ON (a.stop=stop_a.id)
                        JOIN stops stop_b ON (b.stop=stop_b.id)
                        WHERE stop_a.name = 'Sighthill' AND 
                              stop_b.name <> 'Sighthill') AS second_route)B
    ON (A.name_transfer = B.name_transfer)

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

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