繁体   English   中英

sql 查询用于从表中返回某个列

[英]sql query for returning a certain column from a table

我有两张表,我必须查询才能得到一个结果,即喜欢红头发角色的角色

表格1:

                   Characters
identification       alias           hair
     22          Luke Skywalker      blond
     23             Han Solo         brown
     24          Leia Skywalker      black
     25              Mara Jade       red

表 2:

                         loves
identification  CharacterA_identification   CharacterB_identification
      31                   22                          25
      32                   23                          24

第二个表格表示哪个角色喜欢哪个其他角色,例如卢克·天行者,id 为 22,喜欢 Mara Jade,id 为 25

我想编写一个查询,当且仅当 CharacterB 有红头发时,才返回 CharacterA 喜欢 CharacterB 的所有结果。

这是我的查询,但它只给了我红头发的人,而不是喜欢红头发的人。-->

select C.alias
from   Characters C JOIN Loves L1 JOIN Loves L2
on  C.identification = L1.CharacterA_identification
   AND C.identification = L2.CharacterB_identification 
   AND C.hair = 'red' 
       select C.alias 
       from   Characters C 
       where exists (
                     select 1 from loves L INNER JOIN Characters ch                                       
                              on l.CharacterB_identification=ch.identification   
                     where ch. hair = 'red' 
                     and l.CharacterA_identification =c.identification           
                    )
            

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM