通过内连接或左/右连接替换标量子查询的比较

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 BYGROUP 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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)