Select 来自 3 个带有 IN 子句的表
Select from 3 tables with IN clause
接下来需要显示:来自 table 用户名且职位为 "Admin" 并在 'ABC' 公司 (NAME_COMPANY) 工作的所有 fNAME 和 lNAME,带有 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),
);
如果我没有正确理解你的问题,你需要下面的查询。不确定您为什么要使用 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'
我想你在找:
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'
);
接下来需要显示:来自 table 用户名且职位为 "Admin" 并在 'ABC' 公司 (NAME_COMPANY) 工作的所有 fNAME 和 lNAME,带有 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),
);
如果我没有正确理解你的问题,你需要下面的查询。不确定您为什么要使用 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'
我想你在找:
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'
);