2

我有这个查询

select last_name, job_id, department_name, country_name, hire_date
     , sysdate, salary
from employees e
   , departments d
   , locations l
   , countries c 
where e.department_id = d.department_id
  and d.location_id = l.location_id 
  and l.country_id = c.country_id 
  and upper(last_name) like upper('%&name%')

union 

select last_name, j.job_id, department_name, country_name, start_date
     , end_date, to_number(null) 
from employees e
  , job_history j
  , departments d
  , locations l
  , countries c 
where j.department_id = d.department_id 
  and d.location_id = l.location_id 
  and l.country_id = c.country_id 
  and j.employee_id = e.employee_id 
  and upper(last_name) like upper('%&name%');

正如您在此联合中的两个选择中看到的那样,我们得到 'like upper('%&name%')' 这是不是两次都询问它,而是将它从第一个选择传递到该联合中的第二个?

谢谢你的回答。

4

2 回答 2

1

在大多数数据库中,您可以使用 CTE:

with t as (
      select last_name, job_id, department_name, country_name, hire_date,
             sysdate as thedate, salary, employee_id
      from employees e,
           departments d,
           locations l,
           countries c 
      where e.department_id = d.department_id
            and d.location_id = l.location_id 
            and l.country_id = c.country_id 
            and upper(last_name) like upper('%&name%')
    )
select last_name, job_id, department_name, country_name, hire_date, thedate, salary
from t union 
select last_name, job_id, department_name, country_name, hire_date, thedate, NULL
from t join
     job_history jh
     on jh.employee_id = t.employee_id;

的使用sysdate让我想到了Oracle,它确实支持这种with说法。

顺便说一句,您应该学习使用正确的连接语法,它在子句中使用join关键字。from此查询中有一个示例。

于 2013-05-18T19:11:33.297 回答
0

目前还不完全清楚你在问什么,但我认为答案是“是”。整个查询(两者selectsunion运算符结合)作为单个事务工作。您的参数“&name”的符号替换将应用于这两个地方。

于 2013-05-18T19:01:32.797 回答