通过内部联接或左/右联接替换与标量子查询的比较 [英] Replace comparison to scalar subquery by inner join or left/right join
问题描述
我需要使用内部联接或右/左联接编写此查询,但我不知道如何开始:
I need to write this query using inner joins or right/left joins but I don't know how to start:
select * from radicados where asignado =
(select estudianteid from estudiantes where usuario =
(select usuarioid from usuarios where nombre = $nombre_usuario))
但是我不知道如何对联接执行同样的操作.
But I don't know how to do the same with joins.
我认为这一定是这样的:
I think this must be something like:
select * from radicados inner join usuarios on usuarioid=usuario
推荐答案
您似乎想要这样的东西:
It appears you want something like this:
select radicados.*
from
radicados
join estudiantes
on radicados.asignado = estudiantes.estudianteid
join usarios
on estudiantes.usario = usarios.usarioid
where usarios.nombre = $nombre_usuario
在构造这样的查询时,请从FROM
子句开始.根据它们之间的关系,将包含所需数据的各种表连接在一起.如果需要,添加WHERE
子句,描述要在其中过滤联接结果的所有其他条件.然后根据需要填写SELECT
列表.
In constructing such a query, start with the FROM
clause. Join together the various tables containing the needed data, based on the relationships between them. If needed, add a WHERE
clause describing any additional conditions on which you want to filter the result of your join. Then fill in the SELECT
list as appropriate.
在某些情况下,您可能还需要添加其他子句(ORDER BY
,GROUP BY
等),但这在理解基本查询之后还不错.
Under some circumstances you may need to add other clauses, too (ORDER BY
, GROUP BY
, etc.), but that's not bad once you understand basic queries.
这篇关于通过内部联接或左/右联接替换与标量子查询的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!