SQL: 显示两个表的结果
SQL: Display results from two tables
我有两个表:
职位
job_title min_salary job_id max_salary
Public Accountant 4322 1 8777
Assistant 4321 2 9877
President 5432 3 6766
员工
emp_id first_name last_name job_id hire_date
100 Abc Def 1 2020-12-09
101 Xyz Efg 2 2020-10-05
102 Hjk Lmn 3 2019-09-06
job_id 在两个表中都很常见。我想显示助理或总裁员工的名字和姓氏以及加入日期...
我正在尝试使用此获取输出:
SELECT first_name
, last_name
, hire_date
FROM employees
UNION
( SELECT job_title
FROM jobs
WHERE job_title = 'Assistant'
or job_title = 'President');
但是我在选择所需的列时做错了...
谢谢
你应该使用 JOIN:
SELECT first_name, last_name, hire_date
FROM Employees
JOIN Jobs ON Employees.job_id = Jobs.job_id
WHERE job_title IN ('Assistant', 'President');
您需要像这样更改查询:
SELECT *
FROM tutorial.crunchbase_investments_part1
UNION
SELECT *
FROM tutorial.crunchbase_investments_part2
我有两个表:
职位
job_title min_salary job_id max_salary
Public Accountant 4322 1 8777
Assistant 4321 2 9877
President 5432 3 6766
员工
emp_id first_name last_name job_id hire_date
100 Abc Def 1 2020-12-09
101 Xyz Efg 2 2020-10-05
102 Hjk Lmn 3 2019-09-06
job_id 在两个表中都很常见。我想显示助理或总裁员工的名字和姓氏以及加入日期...
我正在尝试使用此获取输出:
SELECT first_name
, last_name
, hire_date
FROM employees
UNION
( SELECT job_title
FROM jobs
WHERE job_title = 'Assistant'
or job_title = 'President');
但是我在选择所需的列时做错了...
谢谢
你应该使用 JOIN:
SELECT first_name, last_name, hire_date
FROM Employees
JOIN Jobs ON Employees.job_id = Jobs.job_id
WHERE job_title IN ('Assistant', 'President');
您需要像这样更改查询:
SELECT *
FROM tutorial.crunchbase_investments_part1
UNION
SELECT *
FROM tutorial.crunchbase_investments_part2