Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用内部连接(MySQL)连接两个查询?_Mysql_Sql - Fatal编程技术网

如何使用内部连接(MySQL)连接两个查询?

如何使用内部连接(MySQL)连接两个查询?,mysql,sql,Mysql,Sql,我有两个查询z1和z2,我想使用名称作为连接器将它们合并为一个查询 select * from (SELECT a.num, a.company, stopb.name FROM route a JOIN route b ON (a.company=b.company AND a.num=b.num<br />) JOIN stops stopa ON (a.stop=stopa.id) JOIN stops stopb ON (b.stop=stopb.id) whe

我有两个查询z1和z2,我想使用名称作为连接器将它们合并为一个查询

select * from 
(SELECT a.num, a.company, stopb.name
FROM route a JOIN route b ON
  (a.company=b.company AND a.num=b.num<br />)
  JOIN stops stopa ON (a.stop=stopa.id)
  JOIN stops stopb ON (b.stop=stopb.id)
where stopa.name = 'Craiglockhart') z1

join 

(SELECT a.num,a.company, stopa.name
FROM route a JOIN route b ON
  (a.company=b.company AND a.num=b.num)
  JOIN stops stopa ON (a.stop=stopa.id)
  JOIN stops stopb ON (b.stop=stopb.id)
where stopb.name = 'Lochend') z2 on z1.name = z2.name;
为什么我会得到一个错误,SELECT将检查超过MAX_JOIN_SIZE的行;检查您的位置并使用SET SQL\u BIG\u SELECTS=1或SET MAX\u JOIN\u SIZE=如果选择正确


如何正确执行?

我猜查询z1和z2的行数非常多,因为它们的名称不唯一

例如,如果与name关联的company和num是唯一的,则应该能够通过向查询中添加DISTINCT来获得结果

select * from 
(SELECT DISTINCT a.num, a.company, stopb.name
FROM route a JOIN route b ON
  (a.company=b.company AND a.num=b.num)
  JOIN stops stopa ON (a.stop=stopa.id)
  JOIN stops stopb ON (b.stop=stopb.id)
where stopa.name = 'Craiglockhart') z1


join 
(SELECT DISTINCT a.num,a.company, stopa.name
FROM route a JOIN route b ON
  (a.company=b.company AND a.num=b.num)
  JOIN stops stopa ON (a.stop=stopa.id)
  JOIN stops stopb ON (b.stop=stopb.id)
where stopb.name = 'Lochend') z2 on z1.name = z2.name;
查询z1和z2非常相似,因此另一种方法可能更简单。
但是,要做到这一点,您需要为查询提供表定义和示例数据。

可能有更有效的方法来编写此代码。样本数据、预期结果和逻辑解释将有所帮助。
select * from 
(SELECT DISTINCT a.num, a.company, stopb.name
FROM route a JOIN route b ON
  (a.company=b.company AND a.num=b.num)
  JOIN stops stopa ON (a.stop=stopa.id)
  JOIN stops stopb ON (b.stop=stopb.id)
where stopa.name = 'Craiglockhart') z1


join 
(SELECT DISTINCT a.num,a.company, stopa.name
FROM route a JOIN route b ON
  (a.company=b.company AND a.num=b.num)
  JOIN stops stopa ON (a.stop=stopa.id)
  JOIN stops stopb ON (b.stop=stopb.id)
where stopb.name = 'Lochend') z2 on z1.name = z2.name;