【问题标题】:Select from 3 tables with IN clause从 3 个带有 IN 子句的表中选择
【发布时间】:2019-11-11 16:58:53
【问题描述】:

需要显示下一个:来自表用户名的所有 fNAME 和 lNAME,其位置为“Admin”并在 'ABC' 公司 (NAME_COMPANY) 工作,带有 IN 子句。

create table company 
(
    CODE_COMPANY char(30),
    NAME_COMPANY varchar2(30) not null,
    MAIL_COMPANY varchar2(30) null,

    constraint PK_CODE_COMPANY primary key (CODE_COMPANY),
);

create table USERNAME 
(
    NAME_USERNAME varchar2(30),
    USER_LOCATION number,
    fNAME varchar2 (30) not null,
    lNAME varchar2 (30) not null,
    PHONE_USER char(13) null,
    USER_POSITION varchar2 (30),
    check (USER_POSITION in('Admin', 'Superadmin', 'Technician', 'Student')),

    constraint PK_NAME_USERNAME primary key (NAME_USERNAME),
    constraint FK_USER_LOCATION foreign key (USER_LOCATION) references uLOCATION (LOCATION)
);

create table uLOCATION 
(
    LOCATION number,
    CODE_COMPANY char(30),
    NAME_LOCATION varchar2(30) not null,
    FLOOR_LOCATION varchar2(10),
    check (FLOOR_LOCATION in ('MAIN_FLOOR', '1ST FLOOR', '2ND FLOOR', '3RD FLOOR')),

    constraint PK_LOCATION primary key (LOCATION),
    constraint FK_CODE_COMPANY_L foreign key (CODE_COMPANY) references company (CODE_COMPANY),
);

【问题讨论】:

    标签: sql in-clause


    【解决方案1】:

    我想你正在寻找:

    select u.*
    from username u
    where u.user_position = 'admin' and
          u.ulocation in (select l.location
                          from ulocation l join
                               ucompany c
                               on l.code_company = c.code_company
                          where c.name_company = 'ABC'
                         );
    

    【讨论】:

      【解决方案2】:

      如果我正确理解您的问题,您需要以下查询。不知道为什么你会使用 IN 而不是标准 = 但是我已经将两者都包含在一个评论中。

      select
          user.fName,
          user.lName
      from
          username as user
          inner join ulocation as location
              on location.location = user.user_location
          inner join company as company
              on company.code_company = location.code_company
      where
          user.user_position = 'Admin'
          and name_company in ('A','B','C') -- not needed if only checking for one company
                                            -- if only one company, change to: name_company = 'ABC'
      

      【讨论】:

      • 需要使用IN代替inner join。有可能吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      相关资源
      最近更新 更多