如何简化查询以检索 Oracle 中的详细信息?
How to simplify the query to retrieve the details in Oracle?
我需要在 Oracle 中编写一个查询来检索在销售、纺织和市场营销部门工作的员工的详细信息。但是当员工在市场部工作时,我只需要获得部门位置在美国和欧盟的员工。
销售和纺织品的位置列在部门 table 中为 NULL。
例如,
部门table:
dept_id emp_id dep_name location
D_1 A1130 MARKETING US
D_2 A1132 SALES (null)
D_3 A1133 TEXTILES (null)
D_4 A1134 MARKETING US
D_5 A1135 MARKETING EU
我写了下面的查询。有没有其他替代方法可以简单地编写它而不是使用 UNION 并编写两次代码?
请帮帮我
SELECT emp.emp_id,
emp.first_name ||' '|| emp.last_name employee_name,
dept.department_name
FROM department dept,
employee emp,
salary sal
WHERE emp.emp_id = dept.emp_id
and dept.dept_id = sal.dept_id
and emp.emp_id = 'A1130'
and dept.department_name in ( 'SALES',
'TEXTILES')
UNION
SELECT emp.emp_id,
emp.first_name ||' '|| emp.last_name employee_name,
dept.department_name
FROM department dept,
employee emp,
salary sal
WHERE emp.emp_id = dept.emp_id
and dept.dept_id = sal.dept_id
and emp.emp_id = 'A1130'
and dept.department_name in ( 'MARKETING')
and dept.locations in ('US','EU');
retrieve the details of employees who work either in Sales, Textiles and Marketing. But when the employees work in Marketing, I need to get only those employees whose department location is US and EU.
您可以在没有 UNION
的情况下执行此操作,在 WHERE
子句中使用 OR
ed 条件:
SELECT
emp.emp_id,
emp.first_name ||' '|| emp.last_name employee_name,
dept.department_name
FROM
department dept
INNER JOIN employee emp ON emp.emp_id = dept.emp_id
INNER JOIN salary sal ON dept.dept_id = sal.dept_id
WHERE
emp.emp_id = 'A1130'
AND (
dept.department_name in ('SALES', 'TEXTILES')
OR (dept.department_name = 'MARKETING' AND dept.locations in ('US','EU'))
)
旁注:始终 使用显式连接(使用 ON
关键字)而不是老式的隐式连接(使用 table 列表在 FROM
条款中),二十多年前就失宠了。
我需要在 Oracle 中编写一个查询来检索在销售、纺织和市场营销部门工作的员工的详细信息。但是当员工在市场部工作时,我只需要获得部门位置在美国和欧盟的员工。
销售和纺织品的位置列在部门 table 中为 NULL。
例如,
部门table:
dept_id emp_id dep_name location
D_1 A1130 MARKETING US
D_2 A1132 SALES (null)
D_3 A1133 TEXTILES (null)
D_4 A1134 MARKETING US
D_5 A1135 MARKETING EU
我写了下面的查询。有没有其他替代方法可以简单地编写它而不是使用 UNION 并编写两次代码?
请帮帮我
SELECT emp.emp_id,
emp.first_name ||' '|| emp.last_name employee_name,
dept.department_name
FROM department dept,
employee emp,
salary sal
WHERE emp.emp_id = dept.emp_id
and dept.dept_id = sal.dept_id
and emp.emp_id = 'A1130'
and dept.department_name in ( 'SALES',
'TEXTILES')
UNION
SELECT emp.emp_id,
emp.first_name ||' '|| emp.last_name employee_name,
dept.department_name
FROM department dept,
employee emp,
salary sal
WHERE emp.emp_id = dept.emp_id
and dept.dept_id = sal.dept_id
and emp.emp_id = 'A1130'
and dept.department_name in ( 'MARKETING')
and dept.locations in ('US','EU');
retrieve the details of employees who work either in Sales, Textiles and Marketing. But when the employees work in Marketing, I need to get only those employees whose department location is US and EU.
您可以在没有 UNION
的情况下执行此操作,在 WHERE
子句中使用 OR
ed 条件:
SELECT
emp.emp_id,
emp.first_name ||' '|| emp.last_name employee_name,
dept.department_name
FROM
department dept
INNER JOIN employee emp ON emp.emp_id = dept.emp_id
INNER JOIN salary sal ON dept.dept_id = sal.dept_id
WHERE
emp.emp_id = 'A1130'
AND (
dept.department_name in ('SALES', 'TEXTILES')
OR (dept.department_name = 'MARKETING' AND dept.locations in ('US','EU'))
)
旁注:始终 使用显式连接(使用 ON
关键字)而不是老式的隐式连接(使用 table 列表在 FROM
条款中),二十多年前就失宠了。