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


基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01