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.
我想这一定是这样的:
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.
这篇关于通过内连接或左/右连接替换标量子查询的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:通过内连接或左/右连接替换标量子查询的比较


基础教程推荐
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- oracle区分大小写的原因? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01