有没有更好的方法使用内部联接命令进行此mysql查询?

时间:2016-04-21 18:52:29

标签: mysql inner-join relationships

在我的数据库中,我有几个表有“多对多”关系相互引用的表,而我教这个查询的方式是通过嵌入式查询。

我一直在阅读关于“多对多”的关系,以及如何使用“内部联接”查询它们但是,我似乎无法找到适合我正在寻找的查询,并且因为我的数据库将会使用非常大的表我不希望我的代码出现性能问题。

SELECT salones.idSalon, 
       salones.Lat, 
       salones.Long, 
       recursos.idRecurso, 
       recursos.Recurso
FROM salones, 
     recursos, 
     salones_has_recursos
WHERE salones_has_recursos.salones_idSalon=salones.idSalon 
  AND salones_has_recursos.recursos_idRecurso=recursos.idRecurso 
  AND recursos.idRecurso IN (SELECT idRecurso
                             FROM salones_has_recursos
                             WHERE idRecurso IN (SELECT idRecurso
                                                 FROM recursos
                                                 WHERE recurso = 'Audiovisual'
                                                )
                            )

我知道嵌入式查询是性能杀手,但这是我能想出如何从表中获取所需信息的唯一方法。有没有人知道如何使用“内连接”命令获得相同的结果?

The resulting table should look a little like this

This is the all the data content of the tables

3 个答案:

答案 0 :(得分:1)

我会给你一个启动器,希望你能弄清楚它是如何工作的:

此:

select idRecurso from salones_has_recursos where idRecurso in
(select idRecurso from recursos where recursos = 'Audiovisual');

变为:

select 
  s.idRecurso 
from 
  salones_has_recursos s
  inner join recursos r
    on s.idRecurso = r.idRecurso
    where r.recursos = 'Audiovisual';

我无法保证它的工作方式无法测试,但我认为这是您的完整解决方案:

SELECT 
   s.idSalon, 
   s.Lat, 
   s.Long, 
   r.idRecurso, 
   r.Recurso
FROM 
   salones s
   inner join recursos r
     on s.idRecurso = r.idRecurso
     and r.recursos = 'Audiovisual'
   inner join salones_has_recursos sr
     on sr.salones_idSalon=s.idSalon
     and sr.recursos_idRecurso=r.idRecurso;

答案 1 :(得分:0)

尝试这个,没有sqlfiddle就很难测试,但试试看看。

SELECT salones.idSalon, 
       salones.Lat, 
       salones.Long, 
       recursos.idRecurso, 
       recursos.Recurso
FROM salones 
     INNER JOIN salones_has_recursos ON salones_has_recursos.salones_idSalon=salones.idSalon 
     INNER JOIN recursos ON salones_has_recursos.recursos_idRecurso=recursos.idRecurso
     AND recursos.idRecursor = salones_has_recursors.idRecurso
     AND recursos.recurso = 'Audiovisual'

答案 2 :(得分:0)

认为这是等效的JOIN查询。 r1,shr1和r2的连接似乎对我来说不仅仅是“关闭”了;我不确定这是因为我如何转换它的错误,或者它是否是从原始转载的。

SELECT s2.idSalon, 
       s2.Lat, 
       s2.Long, 
       r2.idRecurso, 
       r2.Recurso
FROM recursos AS r1
INNER JOIN salones_has_recursos AS shr1 ON r1.idRecurso = shr1.idRecurso
INNER JOIN recurso AS r2 ON shr1.idRecurso = r2.idRecurso
INNER JOIN salones_has_recursos AS shr2 ON r2.idRecurso = shr2.recursos_idRecurso
INNER JOIN salones AS s2 ON shr2.salones_idSalon=s2.idSalon 
WHERE r1.recurso = 'Audiovisual'
;

也不能100%保证提供与DISTINCT操作中的隐式IN相同的结果。

通过“关闭”我的意思是:

recursos.idRecurso IN (SELECT idRecurso
                             FROM salones_has_recursos
                             WHERE idRecurso IN (SELECT idRecurso
                                                 FROM recursos
                                                 WHERE recurso = 'Audiovisual'
                                                )
                            )

似乎与此相同: recursos.recurso = 'Audiovisual'