SQL查询仅从内部联接中的日期选择年份

时间:2018-04-18 19:16:48

标签: sql oracle

我有两张桌子:

table1_name "emp"  contains "hiredate" column.
table2_name "dept" contains "dname" column.

以下是sql代码:

select extract(year from e.hiredate), 
       dname 
from emp e inner join dept d on e.deptno = d.deptno;

“deptno”是两个表的公共列;

但我只需要特定年份(1980年)从日期栏“hiredate”中选择。

请某人帮帮我

2 个答案:

答案 0 :(得分:3)

您应该使用WHERE

select  dname 
from emp e 
inner join dept d 
  on e.deptno=d.deptno
WHERE e.hiredate >= DATE '1980-01-01' AND e.hiredate < DATE '1981-01-01';
-- index on emp(hiredate) woould increase performance

答案 1 :(得分:1)

EXTRACT中使用与WHERE中相同的SELECT,例如

SQL> select extract(year from e.hiredate) yr,
  2         d.dname
  3  from emp e inner join dept d on e.deptno = d.deptno
  4  where extract(year from e.hiredate) = 1980;

        YR DNAME
---------- --------------
      1980 RESEARCH

SQL>