我想解决这个错误。我在MySQL工作,需要在公共列上连接两个派生表。两个表中的所有列都是表格别名。
Scheme是
stops (id, name)
route (num, company, pos, stop)
其中
stops.id
< ---> route.stop
这些表包含城市之间的公交路线,我想弄清楚从'Craiglockhart'到'Sighthill'的所有路线,这些路线只需要两条公交车(即转机点)。此外,因为route
表不包含停止名称(只有ID),所以我们在派生表中使用一些连接来引用名称停止;这只是一个方便)
所以我试着制作两个派生表。其中一条路线可以吸引人们在'Craiglockhart'和任何不是'Craighartlock'的车站之间以及一个相同的第二个衍生桌而是'Sighthill'。我能够让这两个表单独工作。
但是,当我试图沿着所谓的转运站的公共列(即第一个表中的非Craighartlock站和第二个表中的非Sighthill站)加入它们时,我收到错误。
派生表1 :这样可以正常工作并正确返回表。在这里,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
派生表2 :相同但不同的表别名和站限制。这里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
但是,当我尝试将它们连接到它们共同的`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)
)
我还尝试用ON
替换USING (name_transfer)
,因为我想要加入派生表的列在两个派生表中都被称为。
非常感谢任何帮助!
答案 0 :(得分:3)
在FROM
或JOIN
子句中使用子查询时,需要围绕子查询使用括号。您可以在FROM
子句中使用它,但不能在JOIN
子句中使用它。当您在每个子查询周围添加不必要的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
答案 1 :(得分:0)
试试这个。
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)