这些是我的表
create table employees
(emp_no integer not null,
emp_name char(25),
age integer,
department_id integer not null,
salary integer,
CONSTRAINT employees_pk PRIMARY KEY(emp_no),
CONSTRAINT fk_Department FOREIGN KEY(department_id)
REFERENCES Department(department_id));
insert into employees values ( 15,'sara',30 ,101 ,2000 )
insert into employees values ( 12,'maha',29 ,104 ,3000 )
insert into employees values ( 14,'ahmad',24 , 102,4400 )
insert into employees values ( 11,'ali', 27, 103, 2500)
insert into employees values ( 13,'nora', 35, 101,3500 )
create table Works
(emp_no integer not null,
hurs integer,
department_id integer not null,
CONSTRAINT Works_pk PRIMARY KEY(emp_no),
CONSTRAINT Department_fk FOREIGN KEY(department_id)
REFERENCES Department(department_id));
insert into Works values ( 11,7,103)
insert into Works values (12,9,104)
insert into Works values (13,5,101)
insert into Works values (14,10,102)
insert into Works values (15,8,101)
create table Department
(Department_id integer not null,
dep_name char(50),
CONSTRAINT Department_pk PRIMARY KEY(Department_id));
insert into Department values (101,'computer')
insert into Department values (102,'history')
insert into Department values (103,'english')
insert into Department values (104,'physics')
我想选择员工姓名及其部门名称,这些是我的命令无法正常工作:
select emp_name
from employees
select dep_name
from Department
select emp_name
from employees
union
select dep_name
from Department
select emp_name, dep_name
from Department
cross join employees
SELECT employees.emp_name, Department.dep_name
FROM employees, Department
where employees.emp_name = Department.dep_name
select emp_name, Department.dep_name
from employees
inner JOIN Department on employees.emp_name = Department.dep_name
select
count(distinct employees.emp_name)
from
employees
where
employees.emp_name not in (select distinct Department.dep_name
from Department)
select (employees.emp_name)
from employees
where employees.emp_name not in (select Department.dep_name
from Department)
另外,我想选择员工姓名和工资小于或等于20的薪水总和而且我的命令不起作用:
select
emp_name, sum(salary)
from
employees
union
select
hurs
from
works
where
hurs >= 20
我在哪里出错了
答案 0 :(得分:1)
首先,要让工人'名称及其部门,您应该使用employees
关联表department
和department_id
,而不是名称..
select emp_name,Department.dep_name
from employees inner JOIN Department
on employees.department_id = Department.Department_id
以及工作时间> = 20的员工:
select employees.emp_name
from employees join works
on employees.emp_no=works.emp_no AND works.hurs>=20
及其总薪水:
select sum(employees.salary)
from employees join works
on employees.emp_no=works.emp_no AND works.hurs>=20
注意:inner join
和join
是一回事......
简而言之,您应该意识到使用join
链接表格相对比较直观。
你是根据"相同的"两个表都拥有的列。
[其中一个是另一个(主要)密钥的外键]
您可以显示员工'名字和他们的总薪水如下,但这似乎是多余的..
select employees.emp_name, sum(employees.salary)
from employees join works
on employees.emp_no=works.emp_no AND works.hurs>=20
group by employees.emp_name
答案 1 :(得分:0)
要合并他们必须拥有的两个SELECT
个结果
您实际想要使用的是INNER JOIN
:
SELECT emp_name,sum(salary) FROM employees
INNER JOIN works ON employees.emp_no = works.emp_no
WHERE works.hurs >= 20
答案 2 :(得分:0)
正确的语法如下:
SELECT employees.emp_name,
Department.dep_name
FROM employees, Department
WHERE employees.department_id = Department.department_id
或用速记:
SELECT e.emp_name,
d.dep_name
FROM employees as e, Department as d
WHERE e.department_id = d.department_id
对正在发生的事情的描述:
SELECT
部分应包含您希望用逗号分隔的列列表。您可以显式引用表中的列,如示例所示。
FROM
部分应包含您希望以逗号分隔的表格列表。您可以为表提供简写名称,如第二个示例所示。
WHERE
部分允许您标记两个表之间的相关列。